2 * Sonics Silicon Backplane
3 * PCMCIA-Hostbus related functions
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2007 Michael Buesch <mb@bu3sch.de>
8 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/ssb/ssb.h>
12 #include <linux/delay.h>
14 #include <pcmcia/cs_types.h>
15 #include <pcmcia/cs.h>
16 #include <pcmcia/cistpl.h>
17 #include <pcmcia/ciscode.h>
18 #include <pcmcia/ds.h>
19 #include <pcmcia/cisreg.h>
21 #include "ssb_private.h"
24 int ssb_pcmcia_switch_coreidx(struct ssb_bus
*bus
,
27 struct pcmcia_device
*pdev
= bus
->host_pcmcia
;
35 addr
= (coreidx
* SSB_CORE_SIZE
) + SSB_ENUM_BASE
;
37 reg
.Action
= CS_WRITE
;
39 reg
.Value
= (addr
& 0x0000F000) >> 12;
40 err
= pcmcia_access_configuration_register(pdev
, ®
);
41 if (err
!= CS_SUCCESS
)
44 reg
.Value
= (addr
& 0x00FF0000) >> 16;
45 err
= pcmcia_access_configuration_register(pdev
, ®
);
46 if (err
!= CS_SUCCESS
)
49 reg
.Value
= (addr
& 0xFF000000) >> 24;
50 err
= pcmcia_access_configuration_register(pdev
, ®
);
51 if (err
!= CS_SUCCESS
)
58 err
= pcmcia_access_configuration_register(pdev
, ®
);
59 if (err
!= CS_SUCCESS
)
61 read_addr
|= (reg
.Value
& 0xF) << 12;
63 err
= pcmcia_access_configuration_register(pdev
, ®
);
64 if (err
!= CS_SUCCESS
)
66 read_addr
|= reg
.Value
<< 16;
68 err
= pcmcia_access_configuration_register(pdev
, ®
);
69 if (err
!= CS_SUCCESS
)
71 read_addr
|= reg
.Value
<< 24;
73 cur_core
= (read_addr
- SSB_ENUM_BASE
) / SSB_CORE_SIZE
;
74 if (cur_core
== coreidx
)
77 if (attempts
++ > SSB_BAR0_MAX_RETRIES
)
84 ssb_printk(KERN_ERR PFX
"Failed to switch to core %u\n", coreidx
);
88 int ssb_pcmcia_switch_core(struct ssb_bus
*bus
,
89 struct ssb_device
*dev
)
94 ssb_dprintk(KERN_INFO PFX
95 "Switching to %s core, index %d\n",
96 ssb_core_name(dev
->id
.coreid
),
99 spin_lock_irqsave(&bus
->bar_lock
, flags
);
100 err
= ssb_pcmcia_switch_coreidx(bus
, dev
->core_index
);
102 bus
->mapped_device
= dev
;
103 spin_unlock_irqrestore(&bus
->bar_lock
, flags
);
108 int ssb_pcmcia_switch_segment(struct ssb_bus
*bus
, u8 seg
)
115 assert(seg
== 0 || seg
== 1);
118 spin_lock_irqsave(&bus
->bar_lock
, flags
);
120 reg
.Action
= CS_WRITE
;
122 res
= pcmcia_access_configuration_register(bus
->host_pcmcia
, ®
);
123 if (unlikely(res
!= CS_SUCCESS
))
126 reg
.Action
= CS_READ
;
127 res
= pcmcia_access_configuration_register(bus
->host_pcmcia
, ®
);
128 if (unlikely(res
!= CS_SUCCESS
))
131 if (reg
.Value
== seg
)
134 if (unlikely(attempts
++ > SSB_BAR0_MAX_RETRIES
))
138 bus
->mapped_pcmcia_seg
= seg
;
140 spin_unlock_irqrestore(&bus
->bar_lock
, flags
);
143 ssb_printk(KERN_ERR PFX
"Failed to switch pcmcia segment\n");
148 static inline int do_select_core(struct ssb_bus
*bus
,
149 struct ssb_device
*dev
,
153 u8 need_seg
= (*offset
>= 0x800) ? 1 : 0;
155 if (unlikely(dev
!= bus
->mapped_device
)) {
156 err
= ssb_pcmcia_switch_core(bus
, dev
);
160 if (unlikely(need_seg
!= bus
->mapped_pcmcia_seg
)) {
161 err
= ssb_pcmcia_switch_segment(bus
, need_seg
);
171 static u16
ssb_pcmcia_read16(struct ssb_device
*dev
, u16 offset
)
173 struct ssb_bus
*bus
= dev
->bus
;
176 if (unlikely(do_select_core(bus
, dev
, &offset
)))
178 x
= readw(bus
->mmio
+ offset
);
179 //printk("R16 0x%04X, 0x%04X\n", offset, x);
183 static u32
ssb_pcmcia_read32(struct ssb_device
*dev
, u16 offset
)
185 struct ssb_bus
*bus
= dev
->bus
;
188 if (unlikely(do_select_core(bus
, dev
, &offset
)))
190 x
= readl(bus
->mmio
+ offset
);
191 //printk("R32 0x%04X, 0x%08X\n", offset, x);
195 static void ssb_pcmcia_write16(struct ssb_device
*dev
, u16 offset
, u16 value
)
197 struct ssb_bus
*bus
= dev
->bus
;
199 if (unlikely(do_select_core(bus
, dev
, &offset
)))
201 //printk("W16 0x%04X, 0x%04X\n", offset, value);
202 writew(value
, bus
->mmio
+ offset
);
205 static void ssb_pcmcia_write32(struct ssb_device
*dev
, u16 offset
, u32 value
)
207 struct ssb_bus
*bus
= dev
->bus
;
209 if (unlikely(do_select_core(bus
, dev
, &offset
)))
211 //printk("W32 0x%04X, 0x%08X\n", offset, value);
212 readw(bus
->mmio
+ offset
);
213 writew(value
>> 16, bus
->mmio
+ offset
+ 2);
214 readw(bus
->mmio
+ offset
);
215 writew(value
, bus
->mmio
+ offset
);
218 const struct ssb_bus_ops ssb_pcmcia_ops
= {
219 .read16
= ssb_pcmcia_read16
,
220 .read32
= ssb_pcmcia_read32
,
221 .write16
= ssb_pcmcia_write16
,
222 .write32
= ssb_pcmcia_write32
,
225 int ssb_pcmcia_init(struct ssb_bus
*bus
)
230 if (bus
->bustype
!= SSB_BUSTYPE_PCMCIA
)
233 /* Switch segment to a known state and sync
234 * bus->mapped_pcmcia_seg with hardware state. */
235 ssb_pcmcia_switch_segment(bus
, 0);
237 /* Init IRQ routing */
238 reg
.Action
= CS_READ
;
240 if (bus
->chip_id
== 0x4306)
244 err
= pcmcia_access_configuration_register(bus
->host_pcmcia
, ®
);
245 if (err
!= CS_SUCCESS
)
247 reg
.Action
= CS_WRITE
;
248 reg
.Value
|= 0x04 | 0x01;
249 err
= pcmcia_access_configuration_register(bus
->host_pcmcia
, ®
);
250 if (err
!= CS_SUCCESS
)