add fix from #1516
[openwrt.git] / target / linux / brcm47xx-2.6 / files / drivers / ssb / pcmcia.c
1 /*
2 * Sonics Silicon Backplane
3 * PCMCIA-Hostbus related functions
4 *
5 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright 2007 Michael Buesch <mb@bu3sch.de>
7 *
8 * Licensed under the GNU/GPL. See COPYING for details.
9 */
10
11 #include <linux/ssb/ssb.h>
12 #include <linux/delay.h>
13
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>
20
21 #include "ssb_private.h"
22
23
24 int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus,
25 u8 coreidx)
26 {
27 struct pcmcia_device *pdev = bus->host_pcmcia;
28 int err;
29 int attempts = 0;
30 u32 cur_core;
31 conf_reg_t reg;
32 u32 addr;
33 u32 read_addr;
34
35 addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
36 while (1) {
37 reg.Action = CS_WRITE;
38 reg.Offset = 0x2E;
39 reg.Value = (addr & 0x0000F000) >> 12;
40 err = pcmcia_access_configuration_register(pdev, &reg);
41 if (err != CS_SUCCESS)
42 goto error;
43 reg.Offset = 0x30;
44 reg.Value = (addr & 0x00FF0000) >> 16;
45 err = pcmcia_access_configuration_register(pdev, &reg);
46 if (err != CS_SUCCESS)
47 goto error;
48 reg.Offset = 0x32;
49 reg.Value = (addr & 0xFF000000) >> 24;
50 err = pcmcia_access_configuration_register(pdev, &reg);
51 if (err != CS_SUCCESS)
52 goto error;
53
54 read_addr = 0;
55
56 reg.Action = CS_READ;
57 reg.Offset = 0x2E;
58 err = pcmcia_access_configuration_register(pdev, &reg);
59 if (err != CS_SUCCESS)
60 goto error;
61 read_addr |= (reg.Value & 0xF) << 12;
62 reg.Offset = 0x30;
63 err = pcmcia_access_configuration_register(pdev, &reg);
64 if (err != CS_SUCCESS)
65 goto error;
66 read_addr |= reg.Value << 16;
67 reg.Offset = 0x32;
68 err = pcmcia_access_configuration_register(pdev, &reg);
69 if (err != CS_SUCCESS)
70 goto error;
71 read_addr |= reg.Value << 24;
72
73 cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE;
74 if (cur_core == coreidx)
75 break;
76
77 if (attempts++ > SSB_BAR0_MAX_RETRIES)
78 goto error;
79 udelay(10);
80 }
81
82 return 0;
83 error:
84 ssb_printk(KERN_ERR PFX "Failed to switch to core %u\n", coreidx);
85 return -ENODEV;
86 }
87
88 int ssb_pcmcia_switch_core(struct ssb_bus *bus,
89 struct ssb_device *dev)
90 {
91 int err;
92 unsigned long flags;
93
94 ssb_dprintk(KERN_INFO PFX
95 "Switching to %s core, index %d\n",
96 ssb_core_name(dev->id.coreid),
97 dev->core_index);
98
99 spin_lock_irqsave(&bus->bar_lock, flags);
100 err = ssb_pcmcia_switch_coreidx(bus, dev->core_index);
101 if (!err)
102 bus->mapped_device = dev;
103 spin_unlock_irqrestore(&bus->bar_lock, flags);
104
105 return err;
106 }
107
108 int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg)
109 {
110 int attempts = 0;
111 unsigned long flags;
112 conf_reg_t reg;
113 int res, err = 0;
114
115 assert(seg == 0 || seg == 1);
116 reg.Offset = 0x34;
117 reg.Function = 0;
118 spin_lock_irqsave(&bus->bar_lock, flags);
119 while (1) {
120 reg.Action = CS_WRITE;
121 reg.Value = seg;
122 res = pcmcia_access_configuration_register(bus->host_pcmcia, &reg);
123 if (unlikely(res != CS_SUCCESS))
124 goto error;
125 reg.Value = 0xFF;
126 reg.Action = CS_READ;
127 res = pcmcia_access_configuration_register(bus->host_pcmcia, &reg);
128 if (unlikely(res != CS_SUCCESS))
129 goto error;
130
131 if (reg.Value == seg)
132 break;
133
134 if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES))
135 goto error;
136 udelay(10);
137 }
138 bus->mapped_pcmcia_seg = seg;
139 out_unlock:
140 spin_unlock_irqrestore(&bus->bar_lock, flags);
141 return err;
142 error:
143 ssb_printk(KERN_ERR PFX "Failed to switch pcmcia segment\n");
144 err = -ENODEV;
145 goto out_unlock;
146 }
147
148 static inline int do_select_core(struct ssb_bus *bus,
149 struct ssb_device *dev,
150 u16 *offset)
151 {
152 int err;
153 u8 need_seg = (*offset >= 0x800) ? 1 : 0;
154
155 if (unlikely(dev != bus->mapped_device)) {
156 err = ssb_pcmcia_switch_core(bus, dev);
157 if (unlikely(err))
158 return err;
159 }
160 if (unlikely(need_seg != bus->mapped_pcmcia_seg)) {
161 err = ssb_pcmcia_switch_segment(bus, need_seg);
162 if (unlikely(err))
163 return err;
164 }
165 if (need_seg == 1)
166 *offset -= 0x800;
167
168 return 0;
169 }
170
171 static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset)
172 {
173 struct ssb_bus *bus = dev->bus;
174 u16 x;
175
176 if (unlikely(do_select_core(bus, dev, &offset)))
177 return 0xFFFF;
178 x = readw(bus->mmio + offset);
179 //printk("R16 0x%04X, 0x%04X\n", offset, x);
180 return x;
181 }
182
183 static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset)
184 {
185 struct ssb_bus *bus = dev->bus;
186 u32 x;
187
188 if (unlikely(do_select_core(bus, dev, &offset)))
189 return 0xFFFFFFFF;
190 x = readl(bus->mmio + offset);
191 //printk("R32 0x%04X, 0x%08X\n", offset, x);
192 return x;
193 }
194
195 static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value)
196 {
197 struct ssb_bus *bus = dev->bus;
198
199 if (unlikely(do_select_core(bus, dev, &offset)))
200 return;
201 //printk("W16 0x%04X, 0x%04X\n", offset, value);
202 writew(value, bus->mmio + offset);
203 }
204
205 static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value)
206 {
207 struct ssb_bus *bus = dev->bus;
208
209 if (unlikely(do_select_core(bus, dev, &offset)))
210 return;
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);
216 }
217
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,
223 };
224
225 int ssb_pcmcia_init(struct ssb_bus *bus)
226 {
227 conf_reg_t reg;
228 int err;
229
230 if (bus->bustype != SSB_BUSTYPE_PCMCIA)
231 return 0;
232
233 /* Switch segment to a known state and sync
234 * bus->mapped_pcmcia_seg with hardware state. */
235 ssb_pcmcia_switch_segment(bus, 0);
236
237 /* Init IRQ routing */
238 reg.Action = CS_READ;
239 reg.Function = 0;
240 if (bus->chip_id == 0x4306)
241 reg.Offset = 0x00;
242 else
243 reg.Offset = 0x80;
244 err = pcmcia_access_configuration_register(bus->host_pcmcia, &reg);
245 if (err != CS_SUCCESS)
246 goto error;
247 reg.Action = CS_WRITE;
248 reg.Value |= 0x04 | 0x01;
249 err = pcmcia_access_configuration_register(bus->host_pcmcia, &reg);
250 if (err != CS_SUCCESS)
251 goto error;
252
253 return 0;
254 error:
255 return -ENODEV;
256 }
This page took 0.077773 seconds and 5 git commands to generate.