1
2
3
4
5
6
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/slab.h>
15#include <linux/delay.h>
16#include <linux/pci.h>
17#include <linux/gpio.h>
18
19#include <bcm63xx_regs.h>
20#include <bcm63xx_io.h>
21#include "bcm63xx_pcmcia.h"
22
23#define PFX "bcm63xx_pcmcia: "
24
25#ifdef CONFIG_CARDBUS
26
27
28static struct pci_dev *bcm63xx_cb_dev;
29#endif
30
31
32
33
34static inline u32 pcmcia_readl(struct bcm63xx_pcmcia_socket *skt, u32 off)
35{
36 return bcm_readl(skt->base + off);
37}
38
39static inline void pcmcia_writel(struct bcm63xx_pcmcia_socket *skt,
40 u32 val, u32 off)
41{
42 bcm_writel(val, skt->base + off);
43}
44
45
46
47
48
49
50
51
52static int bcm63xx_pcmcia_sock_init(struct pcmcia_socket *sock)
53{
54 return 0;
55}
56
57
58
59
60
61
62
63static int bcm63xx_pcmcia_suspend(struct pcmcia_socket *sock)
64{
65 return 0;
66}
67
68
69
70
71
72
73
74
75static int bcm63xx_pcmcia_set_socket(struct pcmcia_socket *sock,
76 socket_state_t *state)
77{
78 struct bcm63xx_pcmcia_socket *skt;
79 unsigned long flags;
80 u32 val;
81
82 skt = sock->driver_data;
83
84 spin_lock_irqsave(&skt->lock, flags);
85
86
87
88
89
90 val = pcmcia_readl(skt, PCMCIA_C1_REG);
91 if (state->flags & SS_RESET)
92 val |= PCMCIA_C1_RESET_MASK;
93 else
94 val &= ~PCMCIA_C1_RESET_MASK;
95
96
97 if (skt->card_detected && (skt->card_type & CARD_CARDBUS))
98 val ^= PCMCIA_C1_RESET_MASK;
99
100 pcmcia_writel(skt, val, PCMCIA_C1_REG);
101
102
103 skt->requested_state = *state;
104
105 spin_unlock_irqrestore(&skt->lock, flags);
106
107 return 0;
108}
109
110
111
112
113
114enum {
115 IN_VS1 = (1 << 0),
116 IN_VS2 = (1 << 1),
117 IN_CD1_VS2H = (1 << 2),
118 IN_CD2_VS2H = (1 << 3),
119 IN_CD1_VS1H = (1 << 4),
120 IN_CD2_VS1H = (1 << 5),
121};
122
123static const u8 vscd_to_cardtype[] = {
124
125
126 [IN_VS1 | IN_VS2] = (CARD_PCCARD | CARD_5V),
127
128
129 [IN_VS2] = (CARD_PCCARD | CARD_5V | CARD_3V),
130
131
132 [0] = (CARD_PCCARD | CARD_5V | CARD_3V | CARD_XV),
133
134
135 [IN_VS1 | IN_VS2 | IN_CD1_VS1H] = (CARD_CARDBUS | CARD_3V),
136
137
138 [IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V | CARD_XV),
139
140
141 [IN_VS1 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_3V | CARD_XV | CARD_YV),
142
143
144 [IN_VS1] = (CARD_PCCARD | CARD_XV),
145
146
147 [IN_VS1 | IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V),
148
149
150 [IN_VS1 | IN_VS2 | IN_CD1_VS2H] = (CARD_CARDBUS | CARD_XV | CARD_YV),
151
152
153 [IN_VS1 | IN_VS2 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_YV),
154
155
156 [IN_VS1 | IN_CD1_VS1H] = 0,
157};
158
159
160
161
162static unsigned int __get_socket_status(struct bcm63xx_pcmcia_socket *skt)
163{
164 unsigned int stat;
165 u32 val;
166
167 stat = 0;
168
169
170 val = pcmcia_readl(skt, PCMCIA_C1_REG);
171
172 if (!(val & PCMCIA_C1_CD1_MASK) && !(val & PCMCIA_C1_CD2_MASK))
173 stat |= SS_DETECT;
174
175
176 if ((stat & SS_DETECT) && !skt->card_detected) {
177 unsigned int stat = 0;
178
179
180 val |= PCMCIA_C1_VS1OE_MASK;
181 val |= PCMCIA_C1_VS2OE_MASK;
182 pcmcia_writel(skt, val, PCMCIA_C1_REG);
183
184
185 udelay(10);
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;
189
190
191 val &= ~PCMCIA_C1_VS1OE_MASK;
192 val |= PCMCIA_C1_VS2OE_MASK;
193 pcmcia_writel(skt, val, PCMCIA_C1_REG);
194
195
196 udelay(10);
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;
200
201
202 val |= PCMCIA_C1_VS1OE_MASK;
203 val &= ~PCMCIA_C1_VS2OE_MASK;
204 pcmcia_writel(skt, val, PCMCIA_C1_REG);
205
206
207 udelay(10);
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;
211
212
213 skt->card_type = vscd_to_cardtype[stat];
214 if (!skt->card_type)
215 dev_err(&skt->socket.dev, "unsupported card type\n");
216
217
218 val &= ~(PCMCIA_C1_VS1OE_MASK | PCMCIA_C1_VS2OE_MASK);
219
220
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;
224 else
225 val |= PCMCIA_C1_EN_CARDBUS_MASK;
226
227 pcmcia_writel(skt, val, PCMCIA_C1_REG);
228 }
229 skt->card_detected = (stat & SS_DETECT) ? 1 : 0;
230
231
232 if (skt->card_type & CARD_CARDBUS)
233 stat |= SS_CARDBUS;
234 if (skt->card_type & CARD_3V)
235 stat |= SS_3VCARD;
236 if (skt->card_type & CARD_XV)
237 stat |= SS_XVCARD;
238 stat |= SS_POWERON;
239
240 if (gpio_get_value(skt->pd->ready_gpio))
241 stat |= SS_READY;
242
243 return stat;
244}
245
246
247
248
249static int bcm63xx_pcmcia_get_status(struct pcmcia_socket *sock,
250 unsigned int *status)
251{
252 struct bcm63xx_pcmcia_socket *skt;
253
254 skt = sock->driver_data;
255
256 spin_lock_bh(&skt->lock);
257 *status = __get_socket_status(skt);
258 spin_unlock_bh(&skt->lock);
259
260 return 0;
261}
262
263
264
265
266static void bcm63xx_pcmcia_poll(struct timer_list *t)
267{
268 struct bcm63xx_pcmcia_socket *skt;
269 unsigned int stat, events;
270
271 skt = from_timer(skt, t, timer);
272
273 spin_lock_bh(&skt->lock);
274
275 stat = __get_socket_status(skt);
276
277
278
279 events = (stat ^ skt->old_status) & skt->requested_state.csc_mask;
280 skt->old_status = stat;
281 spin_unlock_bh(&skt->lock);
282
283 if (events)
284 pcmcia_parse_events(&skt->socket, events);
285
286 mod_timer(&skt->timer,
287 jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
288}
289
290static int bcm63xx_pcmcia_set_io_map(struct pcmcia_socket *sock,
291 struct pccard_io_map *map)
292{
293
294
295 return 0;
296}
297
298static int bcm63xx_pcmcia_set_mem_map(struct pcmcia_socket *sock,
299 struct pccard_mem_map *map)
300{
301 struct bcm63xx_pcmcia_socket *skt;
302 struct resource *res;
303
304 skt = sock->driver_data;
305 if (map->flags & MAP_ATTRIB)
306 res = skt->attr_res;
307 else
308 res = skt->common_res;
309
310 map->static_start = res->start + map->card_start;
311 return 0;
312}
313
314static 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,
321};
322
323
324
325
326static int bcm63xx_drv_pcmcia_probe(struct platform_device *pdev)
327{
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;
332 u32 val;
333 int ret;
334
335 skt = kzalloc(sizeof(*skt), GFP_KERNEL);
336 if (!skt)
337 return -ENOMEM;
338 spin_lock_init(&skt->lock);
339 sock = &skt->socket;
340 sock->driver_data = skt;
341
342
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) {
348 ret = -EINVAL;
349 goto err;
350 }
351
352
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")) {
356 ret = -EINVAL;
357 goto err;
358 }
359 skt->reg_res = res;
360
361 skt->base = ioremap(res->start, regmem_size);
362 if (!skt->base) {
363 ret = -ENOMEM;
364 goto err;
365 }
366
367
368 res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
369 iomem_size = resource_size(res);
370 skt->io_base = ioremap(res->start, iomem_size);
371 if (!skt->io_base) {
372 ret = -ENOMEM;
373 goto err;
374 }
375
376
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;
384
385#ifdef CONFIG_CARDBUS
386 sock->cb_dev = bcm63xx_cb_dev;
387 if (bcm63xx_cb_dev)
388 sock->features |= SS_CAP_CARDBUS;
389#endif
390
391
392 sock->map_size = resource_size(skt->common_res);
393
394
395 timer_setup(&skt->timer, bcm63xx_pcmcia_poll, 0);
396
397
398
399
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);
404
405
406
407
408
409
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);
416
417 ret = pcmcia_register_socket(sock);
418 if (ret)
419 goto err;
420
421
422 mod_timer(&skt->timer,
423 jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
424
425 platform_set_drvdata(pdev, skt);
426 return 0;
427
428err:
429 if (skt->io_base)
430 iounmap(skt->io_base);
431 if (skt->base)
432 iounmap(skt->base);
433 if (skt->reg_res)
434 release_mem_region(skt->reg_res->start, regmem_size);
435 kfree(skt);
436 return ret;
437}
438
439static int bcm63xx_drv_pcmcia_remove(struct platform_device *pdev)
440{
441 struct bcm63xx_pcmcia_socket *skt;
442 struct resource *res;
443
444 skt = platform_get_drvdata(pdev);
445 del_timer_sync(&skt->timer);
446 iounmap(skt->base);
447 iounmap(skt->io_base);
448 res = skt->reg_res;
449 release_mem_region(res->start, resource_size(res));
450 kfree(skt);
451 return 0;
452}
453
454struct platform_driver bcm63xx_pcmcia_driver = {
455 .probe = bcm63xx_drv_pcmcia_probe,
456 .remove = bcm63xx_drv_pcmcia_remove,
457 .driver = {
458 .name = "bcm63xx_pcmcia",
459 .owner = THIS_MODULE,
460 },
461};
462
463#ifdef CONFIG_CARDBUS
464static int bcm63xx_cb_probe(struct pci_dev *dev,
465 const struct pci_device_id *id)
466{
467
468 bcm63xx_cb_dev = dev;
469 return platform_driver_register(&bcm63xx_pcmcia_driver);
470}
471
472static void bcm63xx_cb_exit(struct pci_dev *dev)
473{
474 platform_driver_unregister(&bcm63xx_pcmcia_driver);
475 bcm63xx_cb_dev = NULL;
476}
477
478static const struct pci_device_id bcm63xx_cb_table[] = {
479 {
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,
485 .class_mask = ~0,
486 },
487
488 {
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,
494 .class_mask = ~0,
495 },
496
497 { },
498};
499
500MODULE_DEVICE_TABLE(pci, bcm63xx_cb_table);
501
502static 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,
507};
508#endif
509
510
511
512
513
514static int __init bcm63xx_pcmcia_init(void)
515{
516#ifdef CONFIG_CARDBUS
517 return pci_register_driver(&bcm63xx_cardbus_driver);
518#else
519 return platform_driver_register(&bcm63xx_pcmcia_driver);
520#endif
521}
522
523static void __exit bcm63xx_pcmcia_exit(void)
524{
525#ifdef CONFIG_CARDBUS
526 return pci_unregister_driver(&bcm63xx_cardbus_driver);
527#else
528 platform_driver_unregister(&bcm63xx_pcmcia_driver);
529#endif
530}
531
532module_init(bcm63xx_pcmcia_init);
533module_exit(bcm63xx_pcmcia_exit);
534
535MODULE_LICENSE("GPL");
536MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
537MODULE_DESCRIPTION("Linux PCMCIA Card Services: bcm63xx Socket Controller");
538