Commit | Line | Data |
---|---|---|
cd8d3d32 CL |
1 | /* |
2 | * Copyright (C) 2008 Christian Lamparter <chunkeey@web.de> | |
3 | * Copyright 2008 Johannes Berg <johannes@sipsolutions.net> | |
4 | * | |
5 | * This driver is a port from stlc45xx: | |
6 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
20 | * 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/firmware.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/irq.h> | |
29 | #include <linux/spi/spi.h> | |
30 | #include <linux/etherdevice.h> | |
31 | #include <linux/gpio.h> | |
32 | ||
33 | #include "p54spi.h" | |
34 | #include "p54spi_eeprom.h" | |
35 | #include "p54.h" | |
36 | ||
37 | #include "p54common.h" | |
38 | ||
39 | MODULE_FIRMWARE("3826.arm"); | |
40 | MODULE_ALIAS("stlc45xx"); | |
41 | ||
a2116993 CL |
42 | /* |
43 | * gpios should be handled in board files and provided via platform data, | |
44 | * but because it's currently impossible for p54spi to have a header file | |
45 | * in include/linux, let's use module paramaters for now | |
46 | */ | |
47 | ||
48 | static int p54spi_gpio_power = 97; | |
49 | module_param(p54spi_gpio_power, int, 0444); | |
50 | MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line"); | |
51 | ||
52 | static int p54spi_gpio_irq = 87; | |
53 | module_param(p54spi_gpio_irq, int, 0444); | |
54 | MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line"); | |
55 | ||
cd8d3d32 CL |
56 | static void p54spi_spi_read(struct p54s_priv *priv, u8 address, |
57 | void *buf, size_t len) | |
58 | { | |
59 | struct spi_transfer t[2]; | |
60 | struct spi_message m; | |
61 | __le16 addr; | |
62 | ||
63 | /* We first push the address */ | |
64 | addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15); | |
65 | ||
66 | spi_message_init(&m); | |
67 | memset(t, 0, sizeof(t)); | |
68 | ||
69 | t[0].tx_buf = &addr; | |
70 | t[0].len = sizeof(addr); | |
71 | spi_message_add_tail(&t[0], &m); | |
72 | ||
73 | t[1].rx_buf = buf; | |
74 | t[1].len = len; | |
75 | spi_message_add_tail(&t[1], &m); | |
76 | ||
77 | spi_sync(priv->spi, &m); | |
78 | } | |
79 | ||
80 | ||
81 | static void p54spi_spi_write(struct p54s_priv *priv, u8 address, | |
82 | const void *buf, size_t len) | |
83 | { | |
84 | struct spi_transfer t[3]; | |
85 | struct spi_message m; | |
86 | __le16 addr; | |
87 | ||
88 | /* We first push the address */ | |
89 | addr = cpu_to_le16(address << 8); | |
90 | ||
91 | spi_message_init(&m); | |
92 | memset(t, 0, sizeof(t)); | |
93 | ||
94 | t[0].tx_buf = &addr; | |
95 | t[0].len = sizeof(addr); | |
96 | spi_message_add_tail(&t[0], &m); | |
97 | ||
98 | t[1].tx_buf = buf; | |
99 | t[1].len = len; | |
100 | spi_message_add_tail(&t[1], &m); | |
101 | ||
102 | if (len % 2) { | |
103 | __le16 last_word; | |
104 | last_word = cpu_to_le16(((u8 *)buf)[len - 1]); | |
105 | ||
106 | t[2].tx_buf = &last_word; | |
107 | t[2].len = sizeof(last_word); | |
108 | spi_message_add_tail(&t[2], &m); | |
109 | } | |
110 | ||
111 | spi_sync(priv->spi, &m); | |
112 | } | |
113 | ||
114 | static u16 p54spi_read16(struct p54s_priv *priv, u8 addr) | |
115 | { | |
116 | __le16 val; | |
117 | ||
118 | p54spi_spi_read(priv, addr, &val, sizeof(val)); | |
119 | ||
120 | return le16_to_cpu(val); | |
121 | } | |
122 | ||
123 | static u32 p54spi_read32(struct p54s_priv *priv, u8 addr) | |
124 | { | |
125 | __le32 val; | |
126 | ||
127 | p54spi_spi_read(priv, addr, &val, sizeof(val)); | |
128 | ||
129 | return le32_to_cpu(val); | |
130 | } | |
131 | ||
132 | static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val) | |
133 | { | |
134 | p54spi_spi_write(priv, addr, &val, sizeof(val)); | |
135 | } | |
136 | ||
137 | static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val) | |
138 | { | |
139 | p54spi_spi_write(priv, addr, &val, sizeof(val)); | |
140 | } | |
141 | ||
142 | struct p54spi_spi_reg { | |
143 | u16 address; /* __le16 ? */ | |
144 | u16 length; | |
145 | char *name; | |
146 | }; | |
147 | ||
148 | static const struct p54spi_spi_reg p54spi_registers_array[] = | |
149 | { | |
150 | { SPI_ADRS_ARM_INTERRUPTS, 32, "ARM_INT " }, | |
151 | { SPI_ADRS_ARM_INT_EN, 32, "ARM_INT_ENA " }, | |
152 | { SPI_ADRS_HOST_INTERRUPTS, 32, "HOST_INT " }, | |
153 | { SPI_ADRS_HOST_INT_EN, 32, "HOST_INT_ENA" }, | |
154 | { SPI_ADRS_HOST_INT_ACK, 32, "HOST_INT_ACK" }, | |
155 | { SPI_ADRS_GEN_PURP_1, 32, "GP1_COMM " }, | |
156 | { SPI_ADRS_GEN_PURP_2, 32, "GP2_COMM " }, | |
157 | { SPI_ADRS_DEV_CTRL_STAT, 32, "DEV_CTRL_STA" }, | |
158 | { SPI_ADRS_DMA_DATA, 16, "DMA_DATA " }, | |
159 | { SPI_ADRS_DMA_WRITE_CTRL, 16, "DMA_WR_CTRL " }, | |
160 | { SPI_ADRS_DMA_WRITE_LEN, 16, "DMA_WR_LEN " }, | |
161 | { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_WR_BASE " }, | |
162 | { SPI_ADRS_DMA_READ_CTRL, 16, "DMA_RD_CTRL " }, | |
163 | { SPI_ADRS_DMA_READ_LEN, 16, "DMA_RD_LEN " }, | |
164 | { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_RD_BASE " } | |
165 | }; | |
166 | ||
167 | static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, __le32 bits) | |
168 | { | |
169 | int i; | |
170 | __le32 buffer; | |
171 | ||
172 | for (i = 0; i < 2000; i++) { | |
173 | p54spi_spi_read(priv, reg, &buffer, sizeof(buffer)); | |
f74d0f5c | 174 | if ((buffer & bits) == bits) |
cd8d3d32 CL |
175 | return 1; |
176 | ||
177 | msleep(1); | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | ||
4f5cab96 MF |
182 | static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base, |
183 | const void *buf, size_t len) | |
184 | { | |
185 | p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL, | |
186 | cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE)); | |
187 | ||
188 | if (p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL, | |
189 | cpu_to_le32(HOST_ALLOWED)) == 0) { | |
190 | dev_err(&priv->spi->dev, "spi_write_dma not allowed " | |
191 | "to DMA write."); | |
192 | return -EAGAIN; | |
193 | } | |
194 | ||
195 | p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len)); | |
196 | p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base); | |
197 | p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len); | |
198 | return 0; | |
199 | } | |
200 | ||
cd8d3d32 CL |
201 | static int p54spi_request_firmware(struct ieee80211_hw *dev) |
202 | { | |
203 | struct p54s_priv *priv = dev->priv; | |
204 | int ret; | |
205 | ||
206 | /* FIXME: should driver use it's own struct device? */ | |
207 | ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev); | |
208 | ||
209 | if (ret < 0) { | |
210 | dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | ret = p54_parse_firmware(dev, priv->firmware); | |
215 | if (ret) { | |
216 | release_firmware(priv->firmware); | |
217 | return ret; | |
218 | } | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | static int p54spi_request_eeprom(struct ieee80211_hw *dev) | |
224 | { | |
225 | struct p54s_priv *priv = dev->priv; | |
226 | const struct firmware *eeprom; | |
227 | int ret; | |
228 | ||
229 | /* | |
230 | * allow users to customize their eeprom. | |
231 | */ | |
232 | ||
233 | ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev); | |
234 | if (ret < 0) { | |
235 | dev_info(&priv->spi->dev, "loading default eeprom...\n"); | |
236 | ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom, | |
237 | sizeof(p54spi_eeprom)); | |
238 | } else { | |
239 | dev_info(&priv->spi->dev, "loading user eeprom...\n"); | |
240 | ret = p54_parse_eeprom(dev, (void *) eeprom->data, | |
241 | (int)eeprom->size); | |
242 | release_firmware(eeprom); | |
243 | } | |
244 | return ret; | |
245 | } | |
246 | ||
247 | static int p54spi_upload_firmware(struct ieee80211_hw *dev) | |
248 | { | |
249 | struct p54s_priv *priv = dev->priv; | |
5e3af1d2 MF |
250 | unsigned long fw_len, _fw_len; |
251 | unsigned int offset = 0; | |
252 | int err = 0; | |
253 | u8 *fw; | |
254 | ||
255 | fw_len = priv->firmware->size; | |
256 | fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL); | |
257 | if (!fw) | |
258 | return -ENOMEM; | |
cd8d3d32 CL |
259 | |
260 | /* stop the device */ | |
261 | p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16( | |
262 | SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET | | |
263 | SPI_CTRL_STAT_START_HALTED)); | |
264 | ||
265 | msleep(TARGET_BOOT_SLEEP); | |
266 | ||
267 | p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16( | |
268 | SPI_CTRL_STAT_HOST_OVERRIDE | | |
269 | SPI_CTRL_STAT_START_HALTED)); | |
270 | ||
271 | msleep(TARGET_BOOT_SLEEP); | |
272 | ||
cd8d3d32 CL |
273 | while (fw_len > 0) { |
274 | _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE); | |
275 | ||
4f5cab96 MF |
276 | err = p54spi_spi_write_dma(priv, cpu_to_le32( |
277 | ISL38XX_DEV_FIRMWARE_ADDR + offset), | |
278 | (fw + offset), _fw_len); | |
279 | if (err < 0) | |
5e3af1d2 | 280 | goto out; |
cd8d3d32 CL |
281 | |
282 | fw_len -= _fw_len; | |
5e3af1d2 | 283 | offset += _fw_len; |
cd8d3d32 CL |
284 | } |
285 | ||
286 | BUG_ON(fw_len != 0); | |
287 | ||
288 | /* enable host interrupts */ | |
289 | p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, | |
290 | cpu_to_le32(SPI_HOST_INTS_DEFAULT)); | |
291 | ||
292 | /* boot the device */ | |
293 | p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16( | |
294 | SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET | | |
295 | SPI_CTRL_STAT_RAM_BOOT)); | |
296 | ||
297 | msleep(TARGET_BOOT_SLEEP); | |
298 | ||
299 | p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16( | |
300 | SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT)); | |
301 | msleep(TARGET_BOOT_SLEEP); | |
5e3af1d2 MF |
302 | |
303 | out: | |
304 | kfree(fw); | |
305 | return err; | |
cd8d3d32 CL |
306 | } |
307 | ||
308 | static void p54spi_power_off(struct p54s_priv *priv) | |
309 | { | |
a2116993 CL |
310 | disable_irq(gpio_to_irq(p54spi_gpio_irq)); |
311 | gpio_set_value(p54spi_gpio_power, 0); | |
cd8d3d32 CL |
312 | } |
313 | ||
314 | static void p54spi_power_on(struct p54s_priv *priv) | |
315 | { | |
a2116993 CL |
316 | gpio_set_value(p54spi_gpio_power, 1); |
317 | enable_irq(gpio_to_irq(p54spi_gpio_irq)); | |
cd8d3d32 CL |
318 | |
319 | /* | |
320 | * need to wait a while before device can be accessed, the lenght | |
321 | * is just a guess | |
322 | */ | |
323 | msleep(10); | |
324 | } | |
325 | ||
326 | static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val) | |
327 | { | |
328 | p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val)); | |
329 | } | |
330 | ||
331 | static void p54spi_wakeup(struct p54s_priv *priv) | |
332 | { | |
333 | unsigned long timeout; | |
334 | u32 ints; | |
335 | ||
336 | /* wake the chip */ | |
337 | p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS, | |
338 | cpu_to_le32(SPI_TARGET_INT_WAKEUP)); | |
339 | ||
340 | /* And wait for the READY interrupt */ | |
341 | timeout = jiffies + HZ; | |
342 | ||
343 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
344 | while (!(ints & SPI_HOST_INT_READY)) { | |
345 | if (time_after(jiffies, timeout)) | |
346 | goto out; | |
347 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
348 | } | |
349 | ||
350 | p54spi_int_ack(priv, SPI_HOST_INT_READY); | |
351 | ||
352 | out: | |
353 | return; | |
354 | } | |
355 | ||
356 | static inline void p54spi_sleep(struct p54s_priv *priv) | |
357 | { | |
358 | p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS, | |
359 | cpu_to_le32(SPI_TARGET_INT_SLEEP)); | |
360 | } | |
361 | ||
362 | static void p54spi_int_ready(struct p54s_priv *priv) | |
363 | { | |
364 | p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32( | |
365 | SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE)); | |
366 | ||
367 | switch (priv->fw_state) { | |
368 | case FW_STATE_BOOTING: | |
369 | priv->fw_state = FW_STATE_READY; | |
370 | complete(&priv->fw_comp); | |
371 | break; | |
372 | case FW_STATE_RESETTING: | |
373 | priv->fw_state = FW_STATE_READY; | |
374 | /* TODO: reinitialize state */ | |
375 | break; | |
376 | default: | |
377 | break; | |
378 | } | |
379 | } | |
380 | ||
381 | static int p54spi_rx(struct p54s_priv *priv) | |
382 | { | |
383 | struct sk_buff *skb; | |
384 | u16 len; | |
385 | ||
386 | p54spi_wakeup(priv); | |
387 | ||
388 | /* dummy read to flush SPI DMA controller bug */ | |
389 | p54spi_read16(priv, SPI_ADRS_GEN_PURP_1); | |
390 | ||
391 | len = p54spi_read16(priv, SPI_ADRS_DMA_DATA); | |
392 | ||
393 | if (len == 0) { | |
394 | dev_err(&priv->spi->dev, "rx request of zero bytes"); | |
395 | return 0; | |
396 | } | |
397 | ||
398 | skb = dev_alloc_skb(len); | |
399 | if (!skb) { | |
400 | dev_err(&priv->spi->dev, "could not alloc skb"); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, skb_put(skb, len), len); | |
405 | p54spi_sleep(priv); | |
406 | ||
407 | if (p54_rx(priv->hw, skb) == 0) | |
408 | dev_kfree_skb(skb); | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
413 | ||
414 | static irqreturn_t p54spi_interrupt(int irq, void *config) | |
415 | { | |
416 | struct spi_device *spi = config; | |
417 | struct p54s_priv *priv = dev_get_drvdata(&spi->dev); | |
418 | ||
419 | queue_work(priv->hw->workqueue, &priv->work); | |
420 | ||
421 | return IRQ_HANDLED; | |
422 | } | |
423 | ||
424 | static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb) | |
425 | { | |
426 | struct p54_hdr *hdr = (struct p54_hdr *) skb->data; | |
cd8d3d32 CL |
427 | unsigned long timeout; |
428 | int ret = 0; | |
429 | u32 ints; | |
430 | ||
431 | p54spi_wakeup(priv); | |
432 | ||
4f5cab96 MF |
433 | ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len); |
434 | if (ret < 0) | |
435 | goto out; | |
cd8d3d32 CL |
436 | |
437 | timeout = jiffies + 2 * HZ; | |
438 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
439 | while (!(ints & SPI_HOST_INT_WR_READY)) { | |
440 | if (time_after(jiffies, timeout)) { | |
4f5cab96 | 441 | dev_err(&priv->spi->dev, "WR_READY timeout\n"); |
cd8d3d32 CL |
442 | ret = -1; |
443 | goto out; | |
444 | } | |
445 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
446 | } | |
447 | ||
448 | p54spi_int_ack(priv, SPI_HOST_INT_WR_READY); | |
449 | p54spi_sleep(priv); | |
450 | ||
cd8d3d32 CL |
451 | if (FREE_AFTER_TX(skb)) |
452 | p54_free_skb(priv->hw, skb); | |
4f5cab96 | 453 | out: |
cd8d3d32 CL |
454 | return ret; |
455 | } | |
456 | ||
457 | static int p54spi_wq_tx(struct p54s_priv *priv) | |
458 | { | |
459 | struct p54s_tx_info *entry; | |
460 | struct sk_buff *skb; | |
461 | struct ieee80211_tx_info *info; | |
462 | struct p54_tx_info *minfo; | |
463 | struct p54s_tx_info *dinfo; | |
731c6531 | 464 | unsigned long flags; |
cd8d3d32 CL |
465 | int ret = 0; |
466 | ||
731c6531 | 467 | spin_lock_irqsave(&priv->tx_lock, flags); |
cd8d3d32 CL |
468 | |
469 | while (!list_empty(&priv->tx_pending)) { | |
470 | entry = list_entry(priv->tx_pending.next, | |
471 | struct p54s_tx_info, tx_list); | |
472 | ||
473 | list_del_init(&entry->tx_list); | |
474 | ||
731c6531 | 475 | spin_unlock_irqrestore(&priv->tx_lock, flags); |
cd8d3d32 CL |
476 | |
477 | dinfo = container_of((void *) entry, struct p54s_tx_info, | |
478 | tx_list); | |
479 | minfo = container_of((void *) dinfo, struct p54_tx_info, | |
480 | data); | |
481 | info = container_of((void *) minfo, struct ieee80211_tx_info, | |
482 | rate_driver_data); | |
483 | skb = container_of((void *) info, struct sk_buff, cb); | |
484 | ||
485 | ret = p54spi_tx_frame(priv, skb); | |
486 | ||
cd8d3d32 CL |
487 | if (ret < 0) { |
488 | p54_free_skb(priv->hw, skb); | |
731c6531 | 489 | return ret; |
cd8d3d32 | 490 | } |
cd8d3d32 | 491 | |
731c6531 CL |
492 | spin_lock_irqsave(&priv->tx_lock, flags); |
493 | } | |
494 | spin_unlock_irqrestore(&priv->tx_lock, flags); | |
cd8d3d32 CL |
495 | return ret; |
496 | } | |
497 | ||
498 | static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb) | |
499 | { | |
500 | struct p54s_priv *priv = dev->priv; | |
501 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
502 | struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data; | |
503 | struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data; | |
731c6531 | 504 | unsigned long flags; |
cd8d3d32 CL |
505 | |
506 | BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data))); | |
507 | ||
731c6531 | 508 | spin_lock_irqsave(&priv->tx_lock, flags); |
cd8d3d32 | 509 | list_add_tail(&di->tx_list, &priv->tx_pending); |
731c6531 | 510 | spin_unlock_irqrestore(&priv->tx_lock, flags); |
cd8d3d32 CL |
511 | |
512 | queue_work(priv->hw->workqueue, &priv->work); | |
513 | } | |
514 | ||
515 | static void p54spi_work(struct work_struct *work) | |
516 | { | |
517 | struct p54s_priv *priv = container_of(work, struct p54s_priv, work); | |
518 | u32 ints; | |
519 | int ret; | |
520 | ||
521 | mutex_lock(&priv->mutex); | |
522 | ||
523 | if (priv->fw_state == FW_STATE_OFF && | |
524 | priv->fw_state == FW_STATE_RESET) | |
525 | goto out; | |
526 | ||
527 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
528 | ||
529 | if (ints & SPI_HOST_INT_READY) { | |
530 | p54spi_int_ready(priv); | |
531 | p54spi_int_ack(priv, SPI_HOST_INT_READY); | |
532 | } | |
533 | ||
534 | if (priv->fw_state != FW_STATE_READY) | |
535 | goto out; | |
536 | ||
537 | if (ints & SPI_HOST_INT_UPDATE) { | |
538 | p54spi_int_ack(priv, SPI_HOST_INT_UPDATE); | |
539 | ret = p54spi_rx(priv); | |
540 | if (ret < 0) | |
541 | goto out; | |
542 | } | |
543 | if (ints & SPI_HOST_INT_SW_UPDATE) { | |
544 | p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE); | |
545 | ret = p54spi_rx(priv); | |
546 | if (ret < 0) | |
547 | goto out; | |
548 | } | |
549 | ||
550 | ret = p54spi_wq_tx(priv); | |
551 | if (ret < 0) | |
552 | goto out; | |
553 | ||
554 | ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS); | |
555 | ||
556 | out: | |
557 | mutex_unlock(&priv->mutex); | |
558 | } | |
559 | ||
560 | static int p54spi_op_start(struct ieee80211_hw *dev) | |
561 | { | |
562 | struct p54s_priv *priv = dev->priv; | |
563 | unsigned long timeout; | |
564 | int ret = 0; | |
565 | ||
566 | if (mutex_lock_interruptible(&priv->mutex)) { | |
567 | ret = -EINTR; | |
568 | goto out; | |
569 | } | |
570 | ||
571 | priv->fw_state = FW_STATE_BOOTING; | |
572 | ||
573 | p54spi_power_on(priv); | |
574 | ||
575 | ret = p54spi_upload_firmware(dev); | |
576 | if (ret < 0) { | |
577 | p54spi_power_off(priv); | |
578 | goto out_unlock; | |
579 | } | |
580 | ||
581 | mutex_unlock(&priv->mutex); | |
582 | ||
583 | timeout = msecs_to_jiffies(2000); | |
584 | timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp, | |
585 | timeout); | |
586 | if (!timeout) { | |
587 | dev_err(&priv->spi->dev, "firmware boot failed"); | |
588 | p54spi_power_off(priv); | |
589 | ret = -1; | |
590 | goto out; | |
591 | } | |
592 | ||
593 | if (mutex_lock_interruptible(&priv->mutex)) { | |
594 | ret = -EINTR; | |
595 | p54spi_power_off(priv); | |
596 | goto out; | |
597 | } | |
598 | ||
599 | WARN_ON(priv->fw_state != FW_STATE_READY); | |
600 | ||
601 | out_unlock: | |
602 | mutex_unlock(&priv->mutex); | |
603 | ||
604 | out: | |
605 | return ret; | |
606 | } | |
607 | ||
608 | static void p54spi_op_stop(struct ieee80211_hw *dev) | |
609 | { | |
610 | struct p54s_priv *priv = dev->priv; | |
731c6531 | 611 | unsigned long flags; |
cd8d3d32 CL |
612 | |
613 | if (mutex_lock_interruptible(&priv->mutex)) { | |
614 | /* FIXME: how to handle this error? */ | |
615 | return; | |
616 | } | |
617 | ||
618 | WARN_ON(priv->fw_state != FW_STATE_READY); | |
619 | ||
620 | cancel_work_sync(&priv->work); | |
621 | ||
622 | p54spi_power_off(priv); | |
731c6531 | 623 | spin_lock_irqsave(&priv->tx_lock, flags); |
cd8d3d32 | 624 | INIT_LIST_HEAD(&priv->tx_pending); |
731c6531 | 625 | spin_unlock_irqrestore(&priv->tx_lock, flags); |
cd8d3d32 CL |
626 | |
627 | priv->fw_state = FW_STATE_OFF; | |
628 | mutex_unlock(&priv->mutex); | |
629 | } | |
630 | ||
631 | static int __devinit p54spi_probe(struct spi_device *spi) | |
632 | { | |
633 | struct p54s_priv *priv = NULL; | |
634 | struct ieee80211_hw *hw; | |
635 | int ret = -EINVAL; | |
636 | ||
637 | hw = p54_init_common(sizeof(*priv)); | |
638 | if (!hw) { | |
639 | dev_err(&priv->spi->dev, "could not alloc ieee80211_hw"); | |
640 | return -ENOMEM; | |
641 | } | |
642 | ||
643 | priv = hw->priv; | |
644 | priv->hw = hw; | |
645 | dev_set_drvdata(&spi->dev, priv); | |
646 | priv->spi = spi; | |
647 | ||
cd8d3d32 CL |
648 | spi->bits_per_word = 16; |
649 | spi->max_speed_hz = 24000000; | |
650 | ||
651 | ret = spi_setup(spi); | |
652 | if (ret < 0) { | |
653 | dev_err(&priv->spi->dev, "spi_setup failed"); | |
654 | goto err_free_common; | |
655 | } | |
656 | ||
a2116993 | 657 | ret = gpio_request(p54spi_gpio_power, "p54spi power"); |
cd8d3d32 CL |
658 | if (ret < 0) { |
659 | dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret); | |
660 | goto err_free_common; | |
661 | } | |
662 | ||
a2116993 | 663 | ret = gpio_request(p54spi_gpio_irq, "p54spi irq"); |
cd8d3d32 CL |
664 | if (ret < 0) { |
665 | dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret); | |
666 | goto err_free_common; | |
667 | } | |
668 | ||
a2116993 CL |
669 | gpio_direction_output(p54spi_gpio_power, 0); |
670 | gpio_direction_input(p54spi_gpio_irq); | |
cd8d3d32 | 671 | |
a2116993 | 672 | ret = request_irq(gpio_to_irq(p54spi_gpio_irq), |
cd8d3d32 CL |
673 | p54spi_interrupt, IRQF_DISABLED, "p54spi", |
674 | priv->spi); | |
675 | if (ret < 0) { | |
676 | dev_err(&priv->spi->dev, "request_irq() failed"); | |
677 | goto err_free_common; | |
678 | } | |
679 | ||
a2116993 | 680 | set_irq_type(gpio_to_irq(p54spi_gpio_irq), |
cd8d3d32 CL |
681 | IRQ_TYPE_EDGE_RISING); |
682 | ||
a2116993 | 683 | disable_irq(gpio_to_irq(p54spi_gpio_irq)); |
cd8d3d32 CL |
684 | |
685 | INIT_WORK(&priv->work, p54spi_work); | |
686 | init_completion(&priv->fw_comp); | |
687 | INIT_LIST_HEAD(&priv->tx_pending); | |
688 | mutex_init(&priv->mutex); | |
689 | SET_IEEE80211_DEV(hw, &spi->dev); | |
690 | priv->common.open = p54spi_op_start; | |
691 | priv->common.stop = p54spi_op_stop; | |
692 | priv->common.tx = p54spi_op_tx; | |
693 | ||
694 | ret = p54spi_request_firmware(hw); | |
695 | if (ret < 0) | |
696 | goto err_free_common; | |
697 | ||
698 | ret = p54spi_request_eeprom(hw); | |
699 | if (ret) | |
700 | goto err_free_common; | |
701 | ||
2ac71072 CL |
702 | ret = p54_register_common(hw, &priv->spi->dev); |
703 | if (ret) | |
cd8d3d32 | 704 | goto err_free_common; |
cd8d3d32 | 705 | |
cd8d3d32 CL |
706 | return 0; |
707 | ||
708 | err_free_common: | |
709 | p54_free_common(priv->hw); | |
710 | return ret; | |
711 | } | |
712 | ||
713 | static int __devexit p54spi_remove(struct spi_device *spi) | |
714 | { | |
715 | struct p54s_priv *priv = dev_get_drvdata(&spi->dev); | |
716 | ||
717 | ieee80211_unregister_hw(priv->hw); | |
718 | ||
a2116993 | 719 | free_irq(gpio_to_irq(p54spi_gpio_irq), spi); |
cd8d3d32 | 720 | |
a2116993 CL |
721 | gpio_free(p54spi_gpio_power); |
722 | gpio_free(p54spi_gpio_irq); | |
cd8d3d32 CL |
723 | release_firmware(priv->firmware); |
724 | ||
725 | mutex_destroy(&priv->mutex); | |
726 | ||
727 | p54_free_common(priv->hw); | |
728 | ieee80211_free_hw(priv->hw); | |
729 | ||
730 | return 0; | |
731 | } | |
732 | ||
733 | ||
734 | static struct spi_driver p54spi_driver = { | |
735 | .driver = { | |
736 | /* use cx3110x name because board-n800.c uses that for the | |
737 | * SPI port */ | |
738 | .name = "cx3110x", | |
739 | .bus = &spi_bus_type, | |
740 | .owner = THIS_MODULE, | |
741 | }, | |
742 | ||
743 | .probe = p54spi_probe, | |
744 | .remove = __devexit_p(p54spi_remove), | |
745 | }; | |
746 | ||
747 | static int __init p54spi_init(void) | |
748 | { | |
749 | int ret; | |
750 | ||
751 | ret = spi_register_driver(&p54spi_driver); | |
752 | if (ret < 0) { | |
753 | printk(KERN_ERR "failed to register SPI driver: %d", ret); | |
754 | goto out; | |
755 | } | |
756 | ||
757 | out: | |
758 | return ret; | |
759 | } | |
760 | ||
761 | static void __exit p54spi_exit(void) | |
762 | { | |
763 | spi_unregister_driver(&p54spi_driver); | |
764 | } | |
765 | ||
766 | module_init(p54spi_init); | |
767 | module_exit(p54spi_exit); | |
768 | ||
769 | MODULE_LICENSE("GPL"); | |
770 | MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>"); |