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(struct timer_list *t)
268 struct bcm63xx_pcmcia_socket *skt;
269 unsigned int stat, events;
271 skt = from_timer(skt, t, timer);
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;
331 unsigned int regmem_size = 0, iomem_size = 0;
336 skt = kzalloc(sizeof(*skt), GFP_KERNEL);
339 spin_lock_init(&skt->lock);
341 sock->driver_data = skt;
343 /* make sure we have all resources we need */
344 skt->common_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
345 skt->attr_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
346 irq = platform_get_irq(pdev, 0);
347 skt->pd = pdev->dev.platform_data;
348 if (!skt->common_res || !skt->attr_res || (irq < 0) || !skt->pd) {
353 /* remap pcmcia registers */
354 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
355 regmem_size = resource_size(res);
356 if (!request_mem_region(res->start, regmem_size, "bcm63xx_pcmcia")) {
362 skt->base = ioremap(res->start, regmem_size);
368 /* remap io registers */
369 res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
370 iomem_size = resource_size(res);
371 skt->io_base = ioremap(res->start, iomem_size);
377 /* resources are static */
378 sock->resource_ops = &pccard_static_ops;
379 sock->ops = &bcm63xx_pcmcia_operations;
380 sock->owner = THIS_MODULE;
381 sock->dev.parent = &pdev->dev;
382 sock->features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
383 sock->io_offset = (unsigned long)skt->io_base;
386 #ifdef CONFIG_CARDBUS
387 sock->cb_dev = bcm63xx_cb_dev;
389 sock->features |= SS_CAP_CARDBUS;
392 /* assume common & attribute memory have the same size */
393 sock->map_size = resource_size(skt->common_res);
395 /* initialize polling timer */
396 timer_setup(&skt->timer, bcm63xx_pcmcia_poll, 0);
398 /* initialize pcmcia control register, drive VS[12] to 0,
399 * leave CB IDSEL to the old value since it is set by the PCI
401 val = pcmcia_readl(skt, PCMCIA_C1_REG);
402 val &= PCMCIA_C1_CBIDSEL_MASK;
403 val |= PCMCIA_C1_EN_PCMCIA_GPIO_MASK;
404 pcmcia_writel(skt, val, PCMCIA_C1_REG);
407 * Hardware has only one set of timings registers, not one for
408 * each memory access type, so we configure them for the
409 * slowest one: attribute memory.
411 val = PCMCIA_C2_DATA16_MASK;
412 val |= 10 << PCMCIA_C2_RWCOUNT_SHIFT;
413 val |= 6 << PCMCIA_C2_INACTIVE_SHIFT;
414 val |= 3 << PCMCIA_C2_SETUP_SHIFT;
415 val |= 3 << PCMCIA_C2_HOLD_SHIFT;
416 pcmcia_writel(skt, val, PCMCIA_C2_REG);
418 ret = pcmcia_register_socket(sock);
422 /* start polling socket */
423 mod_timer(&skt->timer,
424 jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
426 platform_set_drvdata(pdev, skt);
431 iounmap(skt->io_base);
435 release_mem_region(skt->reg_res->start, regmem_size);
440 static int bcm63xx_drv_pcmcia_remove(struct platform_device *pdev)
442 struct bcm63xx_pcmcia_socket *skt;
443 struct resource *res;
445 skt = platform_get_drvdata(pdev);
446 timer_shutdown_sync(&skt->timer);
448 iounmap(skt->io_base);
450 release_mem_region(res->start, resource_size(res));
455 struct platform_driver bcm63xx_pcmcia_driver = {
456 .probe = bcm63xx_drv_pcmcia_probe,
457 .remove = bcm63xx_drv_pcmcia_remove,
459 .name = "bcm63xx_pcmcia",
460 .owner = THIS_MODULE,
464 #ifdef CONFIG_CARDBUS
465 static int bcm63xx_cb_probe(struct pci_dev *dev,
466 const struct pci_device_id *id)
468 /* keep pci device */
469 bcm63xx_cb_dev = dev;
470 return platform_driver_register(&bcm63xx_pcmcia_driver);
473 static void bcm63xx_cb_exit(struct pci_dev *dev)
475 platform_driver_unregister(&bcm63xx_pcmcia_driver);
476 bcm63xx_cb_dev = NULL;
479 static const struct pci_device_id bcm63xx_cb_table[] = {
481 .vendor = PCI_VENDOR_ID_BROADCOM,
482 .device = BCM6348_CPU_ID,
483 .subvendor = PCI_VENDOR_ID_BROADCOM,
484 .subdevice = PCI_ANY_ID,
485 .class = PCI_CLASS_BRIDGE_CARDBUS << 8,
490 .vendor = PCI_VENDOR_ID_BROADCOM,
491 .device = BCM6358_CPU_ID,
492 .subvendor = PCI_VENDOR_ID_BROADCOM,
493 .subdevice = PCI_ANY_ID,
494 .class = PCI_CLASS_BRIDGE_CARDBUS << 8,
501 MODULE_DEVICE_TABLE(pci, bcm63xx_cb_table);
503 static struct pci_driver bcm63xx_cardbus_driver = {
504 .name = "bcm63xx_cardbus",
505 .id_table = bcm63xx_cb_table,
506 .probe = bcm63xx_cb_probe,
507 .remove = bcm63xx_cb_exit,
512 * if cardbus support is enabled, register our platform device after
513 * our fake cardbus bridge has been registered
515 static int __init bcm63xx_pcmcia_init(void)
517 #ifdef CONFIG_CARDBUS
518 return pci_register_driver(&bcm63xx_cardbus_driver);
520 return platform_driver_register(&bcm63xx_pcmcia_driver);
524 static void __exit bcm63xx_pcmcia_exit(void)
526 #ifdef CONFIG_CARDBUS
527 return pci_unregister_driver(&bcm63xx_cardbus_driver);
529 platform_driver_unregister(&bcm63xx_pcmcia_driver);
533 module_init(bcm63xx_pcmcia_init);
534 module_exit(bcm63xx_pcmcia_exit);
536 MODULE_LICENSE("GPL");
537 MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
538 MODULE_DESCRIPTION("Linux PCMCIA Card Services: bcm63xx Socket Controller");