blob: 581dfdedd436f2f9c66c82e3312b59508f3f1a10 [file] [log] [blame]
Solomon Peachya910e4a2013-05-24 20:04:38 -04001/*
2 * Firmware I/O code for mac80211 ST-Ericsson CW1200 drivers
3 *
4 * Copyright (c) 2010, ST-Ericsson
5 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
6 *
7 * Based on:
8 * ST-Ericsson UMAC CW1200 driver which is
9 * Copyright (c) 2010, ST-Ericsson
10 * Author: Ajitpal Singh <ajitpal.singh@stericsson.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Solomon Peachya910e4a2013-05-24 20:04:38 -040017#include <linux/vmalloc.h>
18#include <linux/sched.h>
19#include <linux/firmware.h>
20
21#include "cw1200.h"
22#include "fwio.h"
23#include "hwio.h"
Solomon Peachy911373c2013-06-01 08:08:42 -040024#include "hwbus.h"
Solomon Peachya910e4a2013-05-24 20:04:38 -040025#include "bh.h"
26
27static int cw1200_get_hw_type(u32 config_reg_val, int *major_revision)
28{
29 int hw_type = -1;
30 u32 silicon_type = (config_reg_val >> 24) & 0x7;
31 u32 silicon_vers = (config_reg_val >> 31) & 0x1;
32
33 switch (silicon_type) {
34 case 0x00:
35 *major_revision = 1;
36 hw_type = HIF_9000_SILICON_VERSATILE;
37 break;
38 case 0x01:
39 case 0x02: /* CW1x00 */
40 case 0x04: /* CW1x60 */
41 *major_revision = silicon_type;
42 if (silicon_vers)
43 hw_type = HIF_8601_VERSATILE;
44 else
45 hw_type = HIF_8601_SILICON;
46 break;
47 default:
48 break;
49 }
50
51 return hw_type;
52}
53
54static int cw1200_load_firmware_cw1200(struct cw1200_common *priv)
55{
56 int ret, block, num_blocks;
57 unsigned i;
58 u32 val32;
59 u32 put = 0, get = 0;
60 u8 *buf = NULL;
61 const char *fw_path;
62 const struct firmware *firmware = NULL;
63
64 /* Macroses are local. */
65#define APB_WRITE(reg, val) \
66 do { \
67 ret = cw1200_apb_write_32(priv, CW1200_APB(reg), (val)); \
68 if (ret < 0) \
69 goto error; \
70 } while (0)
71#define APB_READ(reg, val) \
72 do { \
73 ret = cw1200_apb_read_32(priv, CW1200_APB(reg), &(val)); \
74 if (ret < 0) \
75 goto error; \
76 } while (0)
77#define REG_WRITE(reg, val) \
78 do { \
79 ret = cw1200_reg_write_32(priv, (reg), (val)); \
80 if (ret < 0) \
81 goto error; \
82 } while (0)
83#define REG_READ(reg, val) \
84 do { \
85 ret = cw1200_reg_read_32(priv, (reg), &(val)); \
86 if (ret < 0) \
87 goto error; \
88 } while (0)
89
90 switch (priv->hw_revision) {
91 case CW1200_HW_REV_CUT10:
92 fw_path = FIRMWARE_CUT10;
93 if (!priv->sdd_path)
94 priv->sdd_path = SDD_FILE_10;
95 break;
96 case CW1200_HW_REV_CUT11:
97 fw_path = FIRMWARE_CUT11;
98 if (!priv->sdd_path)
99 priv->sdd_path = SDD_FILE_11;
100 break;
101 case CW1200_HW_REV_CUT20:
102 fw_path = FIRMWARE_CUT20;
103 if (!priv->sdd_path)
104 priv->sdd_path = SDD_FILE_20;
105 break;
106 case CW1200_HW_REV_CUT22:
107 fw_path = FIRMWARE_CUT22;
108 if (!priv->sdd_path)
109 priv->sdd_path = SDD_FILE_22;
110 break;
111 case CW1X60_HW_REV:
112 fw_path = FIRMWARE_CW1X60;
113 if (!priv->sdd_path)
114 priv->sdd_path = SDD_FILE_CW1X60;
115 break;
116 default:
117 pr_err("Invalid silicon revision %d.\n", priv->hw_revision);
118 return -EINVAL;
119 }
120
121 /* Initialize common registers */
122 APB_WRITE(DOWNLOAD_IMAGE_SIZE_REG, DOWNLOAD_ARE_YOU_HERE);
123 APB_WRITE(DOWNLOAD_PUT_REG, 0);
124 APB_WRITE(DOWNLOAD_GET_REG, 0);
125 APB_WRITE(DOWNLOAD_STATUS_REG, DOWNLOAD_PENDING);
126 APB_WRITE(DOWNLOAD_FLAGS_REG, 0);
127
128 /* Write the NOP Instruction */
129 REG_WRITE(ST90TDS_SRAM_BASE_ADDR_REG_ID, 0xFFF20000);
130 REG_WRITE(ST90TDS_AHB_DPORT_REG_ID, 0xEAFFFFFE);
131
132 /* Release CPU from RESET */
133 REG_READ(ST90TDS_CONFIG_REG_ID, val32);
134 val32 &= ~ST90TDS_CONFIG_CPU_RESET_BIT;
135 REG_WRITE(ST90TDS_CONFIG_REG_ID, val32);
136
137 /* Enable Clock */
138 val32 &= ~ST90TDS_CONFIG_CPU_CLK_DIS_BIT;
139 REG_WRITE(ST90TDS_CONFIG_REG_ID, val32);
140
Solomon Peachya910e4a2013-05-24 20:04:38 -0400141 /* Load a firmware file */
142 ret = request_firmware(&firmware, fw_path, priv->pdev);
143 if (ret) {
144 pr_err("Can't load firmware file %s.\n", fw_path);
145 goto error;
146 }
147
148 buf = kmalloc(DOWNLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
149 if (!buf) {
150 pr_err("Can't allocate firmware load buffer.\n");
151 ret = -ENOMEM;
152 goto error;
153 }
154
155 /* Check if the bootloader is ready */
156 for (i = 0; i < 100; i += 1 + i / 2) {
157 APB_READ(DOWNLOAD_IMAGE_SIZE_REG, val32);
158 if (val32 == DOWNLOAD_I_AM_HERE)
159 break;
160 mdelay(i);
161 } /* End of for loop */
162
163 if (val32 != DOWNLOAD_I_AM_HERE) {
164 pr_err("Bootloader is not ready.\n");
165 ret = -ETIMEDOUT;
166 goto error;
167 }
168
169 /* Calculcate number of download blocks */
170 num_blocks = (firmware->size - 1) / DOWNLOAD_BLOCK_SIZE + 1;
171
172 /* Updating the length in Download Ctrl Area */
173 val32 = firmware->size; /* Explicit cast from size_t to u32 */
174 APB_WRITE(DOWNLOAD_IMAGE_SIZE_REG, val32);
175
176 /* Firmware downloading loop */
177 for (block = 0; block < num_blocks; block++) {
178 size_t tx_size;
179 size_t block_size;
180
181 /* check the download status */
182 APB_READ(DOWNLOAD_STATUS_REG, val32);
183 if (val32 != DOWNLOAD_PENDING) {
184 pr_err("Bootloader reported error %d.\n", val32);
185 ret = -EIO;
186 goto error;
187 }
188
189 /* loop until put - get <= 24K */
190 for (i = 0; i < 100; i++) {
191 APB_READ(DOWNLOAD_GET_REG, get);
192 if ((put - get) <=
193 (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE))
194 break;
195 mdelay(i);
196 }
197
198 if ((put - get) > (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE)) {
199 pr_err("Timeout waiting for FIFO.\n");
200 ret = -ETIMEDOUT;
201 goto error;
202 }
203
204 /* calculate the block size */
Silvan Jegenc8e49552014-02-25 18:12:52 +0100205 tx_size = block_size = min_t(size_t, firmware->size - put,
206 DOWNLOAD_BLOCK_SIZE);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400207
208 memcpy(buf, &firmware->data[put], block_size);
209 if (block_size < DOWNLOAD_BLOCK_SIZE) {
210 memset(&buf[block_size], 0,
211 DOWNLOAD_BLOCK_SIZE - block_size);
212 tx_size = DOWNLOAD_BLOCK_SIZE;
213 }
214
215 /* send the block to sram */
216 ret = cw1200_apb_write(priv,
217 CW1200_APB(DOWNLOAD_FIFO_OFFSET +
218 (put & (DOWNLOAD_FIFO_SIZE - 1))),
219 buf, tx_size);
220 if (ret < 0) {
221 pr_err("Can't write firmware block @ %d!\n",
222 put & (DOWNLOAD_FIFO_SIZE - 1));
223 goto error;
224 }
225
226 /* update the put register */
227 put += block_size;
228 APB_WRITE(DOWNLOAD_PUT_REG, put);
229 } /* End of firmware download loop */
230
231 /* Wait for the download completion */
232 for (i = 0; i < 300; i += 1 + i / 2) {
233 APB_READ(DOWNLOAD_STATUS_REG, val32);
234 if (val32 != DOWNLOAD_PENDING)
235 break;
236 mdelay(i);
237 }
238 if (val32 != DOWNLOAD_SUCCESS) {
239 pr_err("Wait for download completion failed: 0x%.8X\n", val32);
240 ret = -ETIMEDOUT;
241 goto error;
242 } else {
243 pr_info("Firmware download completed.\n");
244 ret = 0;
245 }
246
247error:
248 kfree(buf);
Markus Elfringdf970d32015-02-04 16:32:15 +0100249 release_firmware(firmware);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400250 return ret;
251
252#undef APB_WRITE
253#undef APB_READ
254#undef REG_WRITE
255#undef REG_READ
256}
257
258
259static int config_reg_read(struct cw1200_common *priv, u32 *val)
260{
261 switch (priv->hw_type) {
262 case HIF_9000_SILICON_VERSATILE: {
263 u16 val16;
264 int ret = cw1200_reg_read_16(priv,
265 ST90TDS_CONFIG_REG_ID,
266 &val16);
267 if (ret < 0)
268 return ret;
269 *val = val16;
270 return 0;
271 }
272 case HIF_8601_VERSATILE:
273 case HIF_8601_SILICON:
274 default:
275 cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, val);
276 break;
277 }
278 return 0;
279}
280
281static int config_reg_write(struct cw1200_common *priv, u32 val)
282{
283 switch (priv->hw_type) {
284 case HIF_9000_SILICON_VERSATILE:
285 return cw1200_reg_write_16(priv,
286 ST90TDS_CONFIG_REG_ID,
287 (u16)val);
288 case HIF_8601_VERSATILE:
289 case HIF_8601_SILICON:
290 default:
291 return cw1200_reg_write_32(priv, ST90TDS_CONFIG_REG_ID, val);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400292 }
293 return 0;
294}
295
296int cw1200_load_firmware(struct cw1200_common *priv)
297{
298 int ret;
299 int i;
300 u32 val32;
301 u16 val16;
302 int major_revision = -1;
303
304 /* Read CONFIG Register */
305 ret = cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
306 if (ret < 0) {
307 pr_err("Can't read config register.\n");
308 goto out;
309 }
310
311 if (val32 == 0 || val32 == 0xffffffff) {
312 pr_err("Bad config register value (0x%08x)\n", val32);
313 ret = -EIO;
314 goto out;
315 }
316
317 priv->hw_type = cw1200_get_hw_type(val32, &major_revision);
318 if (priv->hw_type < 0) {
319 pr_err("Can't deduce hardware type.\n");
320 ret = -ENOTSUPP;
321 goto out;
322 }
323
324 /* Set DPLL Reg value, and read back to confirm writes work */
325 ret = cw1200_reg_write_32(priv, ST90TDS_TSET_GEN_R_W_REG_ID,
326 cw1200_dpll_from_clk(priv->hw_refclk));
327 if (ret < 0) {
328 pr_err("Can't write DPLL register.\n");
329 goto out;
330 }
331
332 msleep(20);
333
334 ret = cw1200_reg_read_32(priv,
335 ST90TDS_TSET_GEN_R_W_REG_ID, &val32);
336 if (ret < 0) {
337 pr_err("Can't read DPLL register.\n");
338 goto out;
339 }
340
341 if (val32 != cw1200_dpll_from_clk(priv->hw_refclk)) {
342 pr_err("Unable to initialise DPLL register. Wrote 0x%.8X, Read 0x%.8X.\n",
343 cw1200_dpll_from_clk(priv->hw_refclk), val32);
344 ret = -EIO;
345 goto out;
346 }
347
348 /* Set wakeup bit in device */
349 ret = cw1200_reg_read_16(priv, ST90TDS_CONTROL_REG_ID, &val16);
350 if (ret < 0) {
351 pr_err("set_wakeup: can't read control register.\n");
352 goto out;
353 }
354
355 ret = cw1200_reg_write_16(priv, ST90TDS_CONTROL_REG_ID,
356 val16 | ST90TDS_CONT_WUP_BIT);
357 if (ret < 0) {
358 pr_err("set_wakeup: can't write control register.\n");
359 goto out;
360 }
361
362 /* Wait for wakeup */
363 for (i = 0; i < 300; i += (1 + i / 2)) {
364 ret = cw1200_reg_read_16(priv,
365 ST90TDS_CONTROL_REG_ID, &val16);
366 if (ret < 0) {
367 pr_err("wait_for_wakeup: can't read control register.\n");
368 goto out;
369 }
370
371 if (val16 & ST90TDS_CONT_RDY_BIT)
372 break;
373
374 msleep(i);
375 }
376
377 if ((val16 & ST90TDS_CONT_RDY_BIT) == 0) {
378 pr_err("wait_for_wakeup: device is not responding.\n");
379 ret = -ETIMEDOUT;
380 goto out;
381 }
382
383 switch (major_revision) {
384 case 1:
385 /* CW1200 Hardware detection logic : Check for CUT1.1 */
386 ret = cw1200_ahb_read_32(priv, CW1200_CUT_ID_ADDR, &val32);
387 if (ret) {
388 pr_err("HW detection: can't read CUT ID.\n");
389 goto out;
390 }
391
392 switch (val32) {
393 case CW1200_CUT_11_ID_STR:
394 pr_info("CW1x00 Cut 1.1 silicon detected.\n");
395 priv->hw_revision = CW1200_HW_REV_CUT11;
396 break;
397 default:
398 pr_info("CW1x00 Cut 1.0 silicon detected.\n");
399 priv->hw_revision = CW1200_HW_REV_CUT10;
400 break;
401 }
402
403 /* According to ST-E, CUT<2.0 has busted BA TID0-3.
404 Just disable it entirely...
405 */
406 priv->ba_rx_tid_mask = 0;
407 priv->ba_tx_tid_mask = 0;
408 break;
409 case 2: {
410 u32 ar1, ar2, ar3;
411 ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR, &ar1);
412 if (ret) {
413 pr_err("(1) HW detection: can't read CUT ID\n");
414 goto out;
415 }
416 ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 4, &ar2);
417 if (ret) {
418 pr_err("(2) HW detection: can't read CUT ID.\n");
419 goto out;
420 }
421
422 ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 8, &ar3);
423 if (ret) {
424 pr_err("(3) HW detection: can't read CUT ID.\n");
425 goto out;
426 }
427
428 if (ar1 == CW1200_CUT_22_ID_STR1 &&
429 ar2 == CW1200_CUT_22_ID_STR2 &&
430 ar3 == CW1200_CUT_22_ID_STR3) {
431 pr_info("CW1x00 Cut 2.2 silicon detected.\n");
432 priv->hw_revision = CW1200_HW_REV_CUT22;
433 } else {
434 pr_info("CW1x00 Cut 2.0 silicon detected.\n");
435 priv->hw_revision = CW1200_HW_REV_CUT20;
436 }
437 break;
438 }
439 case 4:
440 pr_info("CW1x60 silicon detected.\n");
441 priv->hw_revision = CW1X60_HW_REV;
442 break;
443 default:
444 pr_err("Unsupported silicon major revision %d.\n",
445 major_revision);
446 ret = -ENOTSUPP;
447 goto out;
448 }
449
450 /* Checking for access mode */
451 ret = config_reg_read(priv, &val32);
452 if (ret < 0) {
453 pr_err("Can't read config register.\n");
454 goto out;
455 }
456
457 if (!(val32 & ST90TDS_CONFIG_ACCESS_MODE_BIT)) {
458 pr_err("Device is already in QUEUE mode!\n");
459 ret = -EINVAL;
460 goto out;
461 }
462
463 switch (priv->hw_type) {
464 case HIF_8601_SILICON:
465 if (priv->hw_revision == CW1X60_HW_REV) {
466 pr_err("Can't handle CW1160/1260 firmware load yet.\n");
467 ret = -ENOTSUPP;
468 goto out;
469 }
470 ret = cw1200_load_firmware_cw1200(priv);
471 break;
472 default:
473 pr_err("Can't perform firmware load for hw type %d.\n",
474 priv->hw_type);
475 ret = -ENOTSUPP;
476 goto out;
477 }
478 if (ret < 0) {
479 pr_err("Firmware load error.\n");
480 goto out;
481 }
482
483 /* Enable interrupt signalling */
Solomon Peachy911373c2013-06-01 08:08:42 -0400484 priv->hwbus_ops->lock(priv->hwbus_priv);
Solomon Peachyc4fb19d2013-09-23 16:00:03 -0400485 ret = __cw1200_irq_enable(priv, 1);
Solomon Peachy911373c2013-06-01 08:08:42 -0400486 priv->hwbus_ops->unlock(priv->hwbus_priv);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400487 if (ret < 0)
488 goto unsubscribe;
489
490 /* Configure device for MESSSAGE MODE */
491 ret = config_reg_read(priv, &val32);
492 if (ret < 0) {
493 pr_err("Can't read config register.\n");
494 goto unsubscribe;
495 }
496 ret = config_reg_write(priv, val32 & ~ST90TDS_CONFIG_ACCESS_MODE_BIT);
497 if (ret < 0) {
498 pr_err("Can't write config register.\n");
499 goto unsubscribe;
500 }
501
502 /* Unless we read the CONFIG Register we are
503 * not able to get an interrupt
504 */
505 mdelay(10);
506 config_reg_read(priv, &val32);
507
508out:
509 return ret;
510
511unsubscribe:
512 /* Disable interrupt signalling */
Solomon Peachy911373c2013-06-01 08:08:42 -0400513 priv->hwbus_ops->lock(priv->hwbus_priv);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400514 ret = __cw1200_irq_enable(priv, 0);
Solomon Peachy911373c2013-06-01 08:08:42 -0400515 priv->hwbus_ops->unlock(priv->hwbus_priv);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400516 return ret;
517}