]>
Commit | Line | Data |
---|---|---|
2f01a1f5 | 1 | /* |
80301cdc | 2 | * This file is part of wl1251 |
2f01a1f5 KV |
3 | * |
4 | * Copyright (C) 2008 Nokia Corporation | |
5 | * | |
6 | * Contact: Kalle Valo <kalle.valo@nokia.com> | |
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 | ||
8e639c06 | 24 | #include <linux/irq.h> |
2f01a1f5 KV |
25 | #include <linux/module.h> |
26 | #include <linux/crc7.h> | |
27 | #include <linux/spi/spi.h> | |
8e639c06 | 28 | #include <linux/spi/wl12xx.h> |
2f01a1f5 | 29 | |
13674118 | 30 | #include "wl1251.h" |
2f01a1f5 | 31 | #include "reg.h" |
ef2f8d45 | 32 | #include "wl1251_spi.h" |
2f01a1f5 | 33 | |
08d9f572 | 34 | static void wl1251_spi_reset(struct wl1251 *wl) |
2f01a1f5 KV |
35 | { |
36 | u8 *cmd; | |
37 | struct spi_transfer t; | |
38 | struct spi_message m; | |
39 | ||
40 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); | |
41 | if (!cmd) { | |
80301cdc | 42 | wl1251_error("could not allocate cmd for spi reset"); |
2f01a1f5 KV |
43 | return; |
44 | } | |
45 | ||
46 | memset(&t, 0, sizeof(t)); | |
47 | spi_message_init(&m); | |
48 | ||
49 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); | |
50 | ||
51 | t.tx_buf = cmd; | |
52 | t.len = WSPI_INIT_CMD_LEN; | |
53 | spi_message_add_tail(&t, &m); | |
54 | ||
55 | spi_sync(wl->spi, &m); | |
56 | ||
80301cdc | 57 | wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
2f01a1f5 KV |
58 | } |
59 | ||
8e639c06 | 60 | static void wl1251_spi_wake(struct wl1251 *wl) |
2f01a1f5 KV |
61 | { |
62 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; | |
63 | struct spi_transfer t; | |
64 | struct spi_message m; | |
65 | ||
66 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); | |
67 | if (!cmd) { | |
80301cdc | 68 | wl1251_error("could not allocate cmd for spi init"); |
2f01a1f5 KV |
69 | return; |
70 | } | |
71 | ||
72 | memset(crc, 0, sizeof(crc)); | |
73 | memset(&t, 0, sizeof(t)); | |
74 | spi_message_init(&m); | |
75 | ||
76 | /* | |
77 | * Set WSPI_INIT_COMMAND | |
78 | * the data is being send from the MSB to LSB | |
79 | */ | |
80 | cmd[2] = 0xff; | |
81 | cmd[3] = 0xff; | |
82 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; | |
83 | cmd[0] = 0; | |
84 | cmd[7] = 0; | |
85 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; | |
86 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; | |
87 | ||
88 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) | |
89 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; | |
90 | else | |
91 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; | |
92 | ||
93 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS | |
94 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; | |
95 | ||
96 | crc[0] = cmd[1]; | |
97 | crc[1] = cmd[0]; | |
98 | crc[2] = cmd[7]; | |
99 | crc[3] = cmd[6]; | |
100 | crc[4] = cmd[5]; | |
101 | ||
102 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; | |
103 | cmd[4] |= WSPI_INIT_CMD_END; | |
104 | ||
105 | t.tx_buf = cmd; | |
106 | t.len = WSPI_INIT_CMD_LEN; | |
107 | spi_message_add_tail(&t, &m); | |
108 | ||
109 | spi_sync(wl->spi, &m); | |
110 | ||
80301cdc | 111 | wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
2f01a1f5 KV |
112 | } |
113 | ||
08d9f572 BC |
114 | static void wl1251_spi_reset_wake(struct wl1251 *wl) |
115 | { | |
116 | wl1251_spi_reset(wl); | |
8e639c06 | 117 | wl1251_spi_wake(wl); |
08d9f572 BC |
118 | } |
119 | ||
08d9f572 BC |
120 | static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, |
121 | size_t len) | |
2f01a1f5 KV |
122 | { |
123 | struct spi_transfer t[3]; | |
124 | struct spi_message m; | |
5262c12d | 125 | u8 *busy_buf; |
56343a3c | 126 | u32 *cmd; |
2f01a1f5 | 127 | |
56343a3c | 128 | cmd = &wl->buffer_cmd; |
5262c12d | 129 | busy_buf = wl->buffer_busyword; |
56343a3c KV |
130 | |
131 | *cmd = 0; | |
132 | *cmd |= WSPI_CMD_READ; | |
133 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; | |
134 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; | |
2f01a1f5 KV |
135 | |
136 | spi_message_init(&m); | |
137 | memset(t, 0, sizeof(t)); | |
138 | ||
56343a3c | 139 | t[0].tx_buf = cmd; |
2f01a1f5 KV |
140 | t[0].len = 4; |
141 | spi_message_add_tail(&t[0], &m); | |
142 | ||
143 | /* Busy and non busy words read */ | |
144 | t[1].rx_buf = busy_buf; | |
80301cdc | 145 | t[1].len = WL1251_BUSY_WORD_LEN; |
2f01a1f5 KV |
146 | spi_message_add_tail(&t[1], &m); |
147 | ||
148 | t[2].rx_buf = buf; | |
149 | t[2].len = len; | |
150 | spi_message_add_tail(&t[2], &m); | |
151 | ||
152 | spi_sync(wl->spi, &m); | |
153 | ||
154 | /* FIXME: check busy words */ | |
155 | ||
80301cdc KV |
156 | wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
157 | wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); | |
2f01a1f5 KV |
158 | } |
159 | ||
08d9f572 BC |
160 | static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, |
161 | size_t len) | |
2f01a1f5 KV |
162 | { |
163 | struct spi_transfer t[2]; | |
164 | struct spi_message m; | |
56343a3c | 165 | u32 *cmd; |
2f01a1f5 | 166 | |
56343a3c KV |
167 | cmd = &wl->buffer_cmd; |
168 | ||
169 | *cmd = 0; | |
170 | *cmd |= WSPI_CMD_WRITE; | |
171 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; | |
172 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; | |
2f01a1f5 KV |
173 | |
174 | spi_message_init(&m); | |
175 | memset(t, 0, sizeof(t)); | |
176 | ||
56343a3c KV |
177 | t[0].tx_buf = cmd; |
178 | t[0].len = sizeof(*cmd); | |
2f01a1f5 KV |
179 | spi_message_add_tail(&t[0], &m); |
180 | ||
181 | t[1].tx_buf = buf; | |
182 | t[1].len = len; | |
183 | spi_message_add_tail(&t[1], &m); | |
184 | ||
185 | spi_sync(wl->spi, &m); | |
186 | ||
80301cdc KV |
187 | wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
188 | wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); | |
2f01a1f5 | 189 | } |
08d9f572 BC |
190 | |
191 | const struct wl1251_if_operations wl1251_spi_ops = { | |
192 | .read = wl1251_spi_read, | |
193 | .write = wl1251_spi_write, | |
194 | .reset = wl1251_spi_reset_wake, | |
195 | }; | |
8e639c06 BC |
196 | |
197 | static int __devinit wl1251_spi_probe(struct spi_device *spi) | |
198 | { | |
199 | struct wl12xx_platform_data *pdata; | |
200 | struct ieee80211_hw *hw; | |
201 | struct wl1251 *wl; | |
202 | int ret; | |
203 | ||
204 | pdata = spi->dev.platform_data; | |
205 | if (!pdata) { | |
206 | wl1251_error("no platform data"); | |
207 | return -ENODEV; | |
208 | } | |
209 | ||
210 | hw = wl1251_alloc_hw(); | |
211 | if (IS_ERR(hw)) | |
212 | return PTR_ERR(hw); | |
213 | ||
214 | wl = hw->priv; | |
215 | ||
216 | SET_IEEE80211_DEV(hw, &spi->dev); | |
217 | dev_set_drvdata(&spi->dev, wl); | |
218 | wl->spi = spi; | |
219 | wl->if_ops = &wl1251_spi_ops; | |
220 | ||
221 | /* This is the only SPI value that we need to set here, the rest | |
222 | * comes from the board-peripherals file */ | |
223 | spi->bits_per_word = 32; | |
224 | ||
225 | ret = spi_setup(spi); | |
226 | if (ret < 0) { | |
227 | wl1251_error("spi_setup failed"); | |
228 | goto out_free; | |
229 | } | |
230 | ||
231 | wl->set_power = pdata->set_power; | |
232 | if (!wl->set_power) { | |
233 | wl1251_error("set power function missing in platform data"); | |
234 | return -ENODEV; | |
235 | } | |
236 | ||
237 | wl->irq = spi->irq; | |
238 | if (wl->irq < 0) { | |
239 | wl1251_error("irq missing in platform data"); | |
240 | return -ENODEV; | |
241 | } | |
242 | ||
243 | ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl); | |
244 | if (ret < 0) { | |
245 | wl1251_error("request_irq() failed: %d", ret); | |
246 | goto out_free; | |
247 | } | |
248 | ||
249 | set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); | |
250 | ||
251 | disable_irq(wl->irq); | |
252 | ||
253 | ret = wl1251_init_ieee80211(wl); | |
254 | if (ret) | |
255 | goto out_irq; | |
256 | ||
257 | return 0; | |
258 | ||
259 | out_irq: | |
260 | free_irq(wl->irq, wl); | |
261 | ||
262 | out_free: | |
263 | ieee80211_free_hw(hw); | |
264 | ||
265 | return ret; | |
266 | } | |
267 | ||
268 | static int __devexit wl1251_spi_remove(struct spi_device *spi) | |
269 | { | |
270 | struct wl1251 *wl = dev_get_drvdata(&spi->dev); | |
271 | ||
272 | wl1251_free_hw(wl); | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | static struct spi_driver wl1251_spi_driver = { | |
278 | .driver = { | |
279 | .name = "wl12xx", | |
280 | .bus = &spi_bus_type, | |
281 | .owner = THIS_MODULE, | |
282 | }, | |
283 | ||
284 | .probe = wl1251_spi_probe, | |
285 | .remove = __devexit_p(wl1251_spi_remove), | |
286 | }; | |
287 | ||
288 | static int __init wl1251_spi_init(void) | |
289 | { | |
290 | int ret; | |
291 | ||
292 | ret = spi_register_driver(&wl1251_spi_driver); | |
293 | if (ret < 0) { | |
294 | wl1251_error("failed to register spi driver: %d", ret); | |
295 | goto out; | |
296 | } | |
297 | ||
298 | out: | |
299 | return ret; | |
300 | } | |
301 | ||
302 | static void __exit wl1251_spi_exit(void) | |
303 | { | |
304 | spi_unregister_driver(&wl1251_spi_driver); | |
305 | ||
306 | wl1251_notice("unloaded"); | |
307 | } | |
308 | ||
309 | module_init(wl1251_spi_init); | |
310 | module_exit(wl1251_spi_exit); | |
311 | ||
312 | MODULE_LICENSE("GPL"); | |
313 | MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>"); | |
314 | MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>"); |