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
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
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/slab.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
17 #include <linux/gpio.h>
19 #include <bcm63xx_regs.h>
20 #include <bcm63xx_io.h>
21 #include "bcm63xx_pcmcia.h"
23 #define PFX "bcm63xx_pcmcia: "
26 /* if cardbus is used, platform device needs reference to actual pci
28 static struct pci_dev
*bcm63xx_cb_dev
;
32 * read/write helper for pcmcia regs
34 static inline u32
pcmcia_readl(struct bcm63xx_pcmcia_socket
*skt
, u32 off
)
36 return bcm_readl(skt
->base
+ off
);
39 static inline void pcmcia_writel(struct bcm63xx_pcmcia_socket
*skt
,
42 bcm_writel(val
, skt
->base
+ off
);
46 * This callback should (re-)initialise the socket, turn on status
47 * interrupts and PCMCIA bus, and wait for power to stabilise so that
48 * the card status signals report correctly.
50 * Hardware cannot do that.
52 static int bcm63xx_pcmcia_sock_init(struct pcmcia_socket
*sock
)
58 * This callback should remove power on the socket, disable IRQs from
59 * the card, turn off status interrupts, and disable the PCMCIA bus.
61 * Hardware cannot do that.
63 static int bcm63xx_pcmcia_suspend(struct pcmcia_socket
*sock
)
69 * Implements the set_socket() operation for the in-kernel PCMCIA
70 * service (formerly SS_SetSocket in Card Services). We more or
71 * less punt all of this work and let the kernel handle the details
72 * of power configuration, reset, &c. We also record the value of
73 * `state' in order to regurgitate it to the PCMCIA core later.
75 static int bcm63xx_pcmcia_set_socket(struct pcmcia_socket
*sock
,
76 socket_state_t
*state
)
78 struct bcm63xx_pcmcia_socket
*skt
;
82 skt
= sock
->driver_data
;
84 spin_lock_irqsave(&skt
->lock
, flags
);
86 /* note: hardware cannot control socket power, so we will
87 * always report SS_POWERON */
89 /* apply socket reset */
90 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
91 if (state
->flags
& SS_RESET
)
92 val
|= PCMCIA_C1_RESET_MASK
;
94 val
&= ~PCMCIA_C1_RESET_MASK
;
96 /* reverse reset logic for cardbus card */
97 if (skt
->card_detected
&& (skt
->card_type
& CARD_CARDBUS
))
98 val
^= PCMCIA_C1_RESET_MASK
;
100 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
102 /* keep requested state for event reporting */
103 skt
->requested_state
= *state
;
105 spin_unlock_irqrestore(&skt
->lock
, flags
);
111 * identity cardtype from VS[12] input, CD[12] input while only VS2 is
112 * floating, and CD[12] input while only VS1 is floating
117 IN_CD1_VS2H
= (1 << 2),
118 IN_CD2_VS2H
= (1 << 3),
119 IN_CD1_VS1H
= (1 << 4),
120 IN_CD2_VS1H
= (1 << 5),
123 static const u8 vscd_to_cardtype
[] = {
125 /* VS1 float, VS2 float */
126 [IN_VS1
| IN_VS2
] = (CARD_PCCARD
| CARD_5V
),
128 /* VS1 grounded, VS2 float */
129 [IN_VS2
] = (CARD_PCCARD
| CARD_5V
| CARD_3V
),
131 /* VS1 grounded, VS2 grounded */
132 [0] = (CARD_PCCARD
| CARD_5V
| CARD_3V
| CARD_XV
),
134 /* VS1 tied to CD1, VS2 float */
135 [IN_VS1
| IN_VS2
| IN_CD1_VS1H
] = (CARD_CARDBUS
| CARD_3V
),
137 /* VS1 grounded, VS2 tied to CD2 */
138 [IN_VS2
| IN_CD2_VS2H
] = (CARD_CARDBUS
| CARD_3V
| CARD_XV
),
140 /* VS1 tied to CD2, VS2 grounded */
141 [IN_VS1
| IN_CD2_VS1H
] = (CARD_CARDBUS
| CARD_3V
| CARD_XV
| CARD_YV
),
143 /* VS1 float, VS2 grounded */
144 [IN_VS1
] = (CARD_PCCARD
| CARD_XV
),
146 /* VS1 float, VS2 tied to CD2 */
147 [IN_VS1
| IN_VS2
| IN_CD2_VS2H
] = (CARD_CARDBUS
| CARD_3V
),
149 /* VS1 float, VS2 tied to CD1 */
150 [IN_VS1
| IN_VS2
| IN_CD1_VS2H
] = (CARD_CARDBUS
| CARD_XV
| CARD_YV
),
152 /* VS1 tied to CD2, VS2 float */
153 [IN_VS1
| IN_VS2
| IN_CD2_VS1H
] = (CARD_CARDBUS
| CARD_YV
),
155 /* VS2 grounded, VS1 is tied to CD1, CD2 is grounded */
156 [IN_VS1
| IN_CD1_VS1H
] = 0, /* ignore cardbay */
160 * poll hardware to check card insertion status
162 static unsigned int __get_socket_status(struct bcm63xx_pcmcia_socket
*skt
)
169 /* check CD for card presence */
170 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
172 if (!(val
& PCMCIA_C1_CD1_MASK
) && !(val
& PCMCIA_C1_CD2_MASK
))
175 /* if new insertion, detect cardtype */
176 if ((stat
& SS_DETECT
) && !skt
->card_detected
) {
177 unsigned int stat
= 0;
179 /* float VS1, float VS2 */
180 val
|= PCMCIA_C1_VS1OE_MASK
;
181 val
|= PCMCIA_C1_VS2OE_MASK
;
182 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
184 /* wait for output to stabilize and read VS[12] */
186 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
187 stat
|= (val
& PCMCIA_C1_VS1_MASK
) ? IN_VS1
: 0;
188 stat
|= (val
& PCMCIA_C1_VS2_MASK
) ? IN_VS2
: 0;
190 /* drive VS1 low, float VS2 */
191 val
&= ~PCMCIA_C1_VS1OE_MASK
;
192 val
|= PCMCIA_C1_VS2OE_MASK
;
193 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
195 /* wait for output to stabilize and read CD[12] */
197 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
198 stat
|= (val
& PCMCIA_C1_CD1_MASK
) ? IN_CD1_VS2H
: 0;
199 stat
|= (val
& PCMCIA_C1_CD2_MASK
) ? IN_CD2_VS2H
: 0;
201 /* float VS1, drive VS2 low */
202 val
|= PCMCIA_C1_VS1OE_MASK
;
203 val
&= ~PCMCIA_C1_VS2OE_MASK
;
204 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
206 /* wait for output to stabilize and read CD[12] */
208 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
209 stat
|= (val
& PCMCIA_C1_CD1_MASK
) ? IN_CD1_VS1H
: 0;
210 stat
|= (val
& PCMCIA_C1_CD2_MASK
) ? IN_CD2_VS1H
: 0;
212 /* guess cardtype from all this */
213 skt
->card_type
= vscd_to_cardtype
[stat
];
215 dev_err(&skt
->socket
.dev
, "unsupported card type\n");
217 /* drive both VS pin to 0 again */
218 val
&= ~(PCMCIA_C1_VS1OE_MASK
| PCMCIA_C1_VS2OE_MASK
);
220 /* enable correct logic */
221 val
&= ~(PCMCIA_C1_EN_PCMCIA_MASK
| PCMCIA_C1_EN_CARDBUS_MASK
);
222 if (skt
->card_type
& CARD_PCCARD
)
223 val
|= PCMCIA_C1_EN_PCMCIA_MASK
;
225 val
|= PCMCIA_C1_EN_CARDBUS_MASK
;
227 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
229 skt
->card_detected
= (stat
& SS_DETECT
) ? 1 : 0;
231 /* report card type/voltage */
232 if (skt
->card_type
& CARD_CARDBUS
)
234 if (skt
->card_type
& CARD_3V
)
236 if (skt
->card_type
& CARD_XV
)
240 if (gpio_get_value(skt
->pd
->ready_gpio
))
247 * core request to get current socket status
249 static int bcm63xx_pcmcia_get_status(struct pcmcia_socket
*sock
,
250 unsigned int *status
)
252 struct bcm63xx_pcmcia_socket
*skt
;
254 skt
= sock
->driver_data
;
256 spin_lock_bh(&skt
->lock
);
257 *status
= __get_socket_status(skt
);
258 spin_unlock_bh(&skt
->lock
);
264 * socket polling timer callback
266 static void bcm63xx_pcmcia_poll(unsigned long data
)
268 struct bcm63xx_pcmcia_socket
*skt
;
269 unsigned int stat
, events
;
271 skt
= (struct bcm63xx_pcmcia_socket
*)data
;
273 spin_lock_bh(&skt
->lock
);
275 stat
= __get_socket_status(skt
);
277 /* keep only changed bits, and mask with required one from the
279 events
= (stat
^ skt
->old_status
) & skt
->requested_state
.csc_mask
;
280 skt
->old_status
= stat
;
281 spin_unlock_bh(&skt
->lock
);
284 pcmcia_parse_events(&skt
->socket
, events
);
286 mod_timer(&skt
->timer
,
287 jiffies
+ msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE
));
290 static int bcm63xx_pcmcia_set_io_map(struct pcmcia_socket
*sock
,
291 struct pccard_io_map
*map
)
293 /* this doesn't seem to be called by pcmcia layer if static
298 static int bcm63xx_pcmcia_set_mem_map(struct pcmcia_socket
*sock
,
299 struct pccard_mem_map
*map
)
301 struct bcm63xx_pcmcia_socket
*skt
;
302 struct resource
*res
;
304 skt
= sock
->driver_data
;
305 if (map
->flags
& MAP_ATTRIB
)
308 res
= skt
->common_res
;
310 map
->static_start
= res
->start
+ map
->card_start
;
314 static struct pccard_operations bcm63xx_pcmcia_operations
= {
315 .init
= bcm63xx_pcmcia_sock_init
,
316 .suspend
= bcm63xx_pcmcia_suspend
,
317 .get_status
= bcm63xx_pcmcia_get_status
,
318 .set_socket
= bcm63xx_pcmcia_set_socket
,
319 .set_io_map
= bcm63xx_pcmcia_set_io_map
,
320 .set_mem_map
= bcm63xx_pcmcia_set_mem_map
,
324 * register pcmcia socket to core
326 static int bcm63xx_drv_pcmcia_probe(struct platform_device
*pdev
)
328 struct bcm63xx_pcmcia_socket
*skt
;
329 struct pcmcia_socket
*sock
;
330 struct resource
*res
, *irq_res
;
331 unsigned int regmem_size
= 0, iomem_size
= 0;
335 skt
= kzalloc(sizeof(*skt
), GFP_KERNEL
);
338 spin_lock_init(&skt
->lock
);
340 sock
->driver_data
= skt
;
342 /* make sure we have all resources we need */
343 skt
->common_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
344 skt
->attr_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
345 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
346 skt
->pd
= pdev
->dev
.platform_data
;
347 if (!skt
->common_res
|| !skt
->attr_res
|| !irq_res
|| !skt
->pd
) {
352 /* remap pcmcia registers */
353 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
354 regmem_size
= resource_size(res
);
355 if (!request_mem_region(res
->start
, regmem_size
, "bcm63xx_pcmcia")) {
361 skt
->base
= ioremap(res
->start
, regmem_size
);
367 /* remap io registers */
368 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
369 iomem_size
= resource_size(res
);
370 skt
->io_base
= ioremap(res
->start
, iomem_size
);
376 /* resources are static */
377 sock
->resource_ops
= &pccard_static_ops
;
378 sock
->ops
= &bcm63xx_pcmcia_operations
;
379 sock
->owner
= THIS_MODULE
;
380 sock
->dev
.parent
= &pdev
->dev
;
381 sock
->features
= SS_CAP_STATIC_MAP
| SS_CAP_PCCARD
;
382 sock
->io_offset
= (unsigned long)skt
->io_base
;
383 sock
->pci_irq
= irq_res
->start
;
385 #ifdef CONFIG_CARDBUS
386 sock
->cb_dev
= bcm63xx_cb_dev
;
388 sock
->features
|= SS_CAP_CARDBUS
;
391 /* assume common & attribute memory have the same size */
392 sock
->map_size
= resource_size(skt
->common_res
);
394 /* initialize polling timer */
395 setup_timer(&skt
->timer
, bcm63xx_pcmcia_poll
, (unsigned long)skt
);
397 /* initialize pcmcia control register, drive VS[12] to 0,
398 * leave CB IDSEL to the old value since it is set by the PCI
400 val
= pcmcia_readl(skt
, PCMCIA_C1_REG
);
401 val
&= PCMCIA_C1_CBIDSEL_MASK
;
402 val
|= PCMCIA_C1_EN_PCMCIA_GPIO_MASK
;
403 pcmcia_writel(skt
, val
, PCMCIA_C1_REG
);
406 * Hardware has only one set of timings registers, not one for
407 * each memory access type, so we configure them for the
408 * slowest one: attribute memory.
410 val
= PCMCIA_C2_DATA16_MASK
;
411 val
|= 10 << PCMCIA_C2_RWCOUNT_SHIFT
;
412 val
|= 6 << PCMCIA_C2_INACTIVE_SHIFT
;
413 val
|= 3 << PCMCIA_C2_SETUP_SHIFT
;
414 val
|= 3 << PCMCIA_C2_HOLD_SHIFT
;
415 pcmcia_writel(skt
, val
, PCMCIA_C2_REG
);
417 ret
= pcmcia_register_socket(sock
);
421 /* start polling socket */
422 mod_timer(&skt
->timer
,
423 jiffies
+ msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE
));
425 platform_set_drvdata(pdev
, skt
);
430 iounmap(skt
->io_base
);
434 release_mem_region(skt
->reg_res
->start
, regmem_size
);
439 static int bcm63xx_drv_pcmcia_remove(struct platform_device
*pdev
)
441 struct bcm63xx_pcmcia_socket
*skt
;
442 struct resource
*res
;
444 skt
= platform_get_drvdata(pdev
);
445 del_timer_sync(&skt
->timer
);
447 iounmap(skt
->io_base
);
449 release_mem_region(res
->start
, resource_size(res
));
454 struct platform_driver bcm63xx_pcmcia_driver
= {
455 .probe
= bcm63xx_drv_pcmcia_probe
,
456 .remove
= bcm63xx_drv_pcmcia_remove
,
458 .name
= "bcm63xx_pcmcia",
459 .owner
= THIS_MODULE
,
463 #ifdef CONFIG_CARDBUS
464 static int bcm63xx_cb_probe(struct pci_dev
*dev
,
465 const struct pci_device_id
*id
)
467 /* keep pci device */
468 bcm63xx_cb_dev
= dev
;
469 return platform_driver_register(&bcm63xx_pcmcia_driver
);
472 static void bcm63xx_cb_exit(struct pci_dev
*dev
)
474 platform_driver_unregister(&bcm63xx_pcmcia_driver
);
475 bcm63xx_cb_dev
= NULL
;
478 static DEFINE_PCI_DEVICE_TABLE(bcm63xx_cb_table
) = {
480 .vendor
= PCI_VENDOR_ID_BROADCOM
,
481 .device
= BCM6348_CPU_ID
,
482 .subvendor
= PCI_VENDOR_ID_BROADCOM
,
483 .subdevice
= PCI_ANY_ID
,
484 .class = PCI_CLASS_BRIDGE_CARDBUS
<< 8,
489 .vendor
= PCI_VENDOR_ID_BROADCOM
,
490 .device
= BCM6358_CPU_ID
,
491 .subvendor
= PCI_VENDOR_ID_BROADCOM
,
492 .subdevice
= PCI_ANY_ID
,
493 .class = PCI_CLASS_BRIDGE_CARDBUS
<< 8,
500 MODULE_DEVICE_TABLE(pci
, bcm63xx_cb_table
);
502 static struct pci_driver bcm63xx_cardbus_driver
= {
503 .name
= "bcm63xx_cardbus",
504 .id_table
= bcm63xx_cb_table
,
505 .probe
= bcm63xx_cb_probe
,
506 .remove
= bcm63xx_cb_exit
,
511 * if cardbus support is enabled, register our platform device after
512 * our fake cardbus bridge has been registered
514 static int __init
bcm63xx_pcmcia_init(void)
516 #ifdef CONFIG_CARDBUS
517 return pci_register_driver(&bcm63xx_cardbus_driver
);
519 return platform_driver_register(&bcm63xx_pcmcia_driver
);
523 static void __exit
bcm63xx_pcmcia_exit(void)
525 #ifdef CONFIG_CARDBUS
526 return pci_unregister_driver(&bcm63xx_cardbus_driver
);
528 platform_driver_unregister(&bcm63xx_pcmcia_driver
);
532 module_init(bcm63xx_pcmcia_init
);
533 module_exit(bcm63xx_pcmcia_exit
);
535 MODULE_LICENSE("GPL");
536 MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
537 MODULE_DESCRIPTION("Linux PCMCIA Card Services: bcm63xx Socket Controller");