2 * Firmware I/O code for mac80211 ST-Ericsson CW1200 drivers
4 * Copyright (c) 2010, ST-Ericsson
5 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
8 * ST-Ericsson UMAC CW1200 driver which is
9 * Copyright (c) 2010, ST-Ericsson
10 * Author: Ajitpal Singh <ajitpal.singh@stericsson.com>
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.
17 #include <linux/vmalloc.h>
18 #include <linux/sched.h>
19 #include <linux/firmware.h>
27 static int cw1200_get_hw_type(u32 config_reg_val
, int *major_revision
)
30 u32 silicon_type
= (config_reg_val
>> 24) & 0x7;
31 u32 silicon_vers
= (config_reg_val
>> 31) & 0x1;
33 switch (silicon_type
) {
36 hw_type
= HIF_9000_SILICON_VERSATILE
;
39 case 0x02: /* CW1x00 */
40 case 0x04: /* CW1x60 */
41 *major_revision
= silicon_type
;
43 hw_type
= HIF_8601_VERSATILE
;
45 hw_type
= HIF_8601_SILICON
;
54 static int cw1200_load_firmware_cw1200(struct cw1200_common
*priv
)
56 int ret
, block
, num_blocks
;
62 const struct firmware
*firmware
= NULL
;
64 /* Macroses are local. */
65 #define APB_WRITE(reg, val) \
67 ret = cw1200_apb_write_32(priv, CW1200_APB(reg), (val)); \
71 #define APB_READ(reg, val) \
73 ret = cw1200_apb_read_32(priv, CW1200_APB(reg), &(val)); \
77 #define REG_WRITE(reg, val) \
79 ret = cw1200_reg_write_32(priv, (reg), (val)); \
83 #define REG_READ(reg, val) \
85 ret = cw1200_reg_read_32(priv, (reg), &(val)); \
90 switch (priv
->hw_revision
) {
91 case CW1200_HW_REV_CUT10
:
92 fw_path
= FIRMWARE_CUT10
;
94 priv
->sdd_path
= SDD_FILE_10
;
96 case CW1200_HW_REV_CUT11
:
97 fw_path
= FIRMWARE_CUT11
;
99 priv
->sdd_path
= SDD_FILE_11
;
101 case CW1200_HW_REV_CUT20
:
102 fw_path
= FIRMWARE_CUT20
;
104 priv
->sdd_path
= SDD_FILE_20
;
106 case CW1200_HW_REV_CUT22
:
107 fw_path
= FIRMWARE_CUT22
;
109 priv
->sdd_path
= SDD_FILE_22
;
112 fw_path
= FIRMWARE_CW1X60
;
114 priv
->sdd_path
= SDD_FILE_CW1X60
;
117 pr_err("Invalid silicon revision %d.\n", priv
->hw_revision
);
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);
128 /* Write the NOP Instruction */
129 REG_WRITE(ST90TDS_SRAM_BASE_ADDR_REG_ID
, 0xFFF20000);
130 REG_WRITE(ST90TDS_AHB_DPORT_REG_ID
, 0xEAFFFFFE);
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
);
138 val32
&= ~ST90TDS_CONFIG_CPU_CLK_DIS_BIT
;
139 REG_WRITE(ST90TDS_CONFIG_REG_ID
, val32
);
141 /* Load a firmware file */
142 ret
= request_firmware(&firmware
, fw_path
, priv
->pdev
);
144 pr_err("Can't load firmware file %s.\n", fw_path
);
148 buf
= kmalloc(DOWNLOAD_BLOCK_SIZE
, GFP_KERNEL
| GFP_DMA
);
150 pr_err("Can't allocate firmware load buffer.\n");
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
)
161 } /* End of for loop */
163 if (val32
!= DOWNLOAD_I_AM_HERE
) {
164 pr_err("Bootloader is not ready.\n");
169 /* Calculcate number of download blocks */
170 num_blocks
= (firmware
->size
- 1) / DOWNLOAD_BLOCK_SIZE
+ 1;
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
);
176 /* Firmware downloading loop */
177 for (block
= 0; block
< num_blocks
; block
++) {
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
);
189 /* loop until put - get <= 24K */
190 for (i
= 0; i
< 100; i
++) {
191 APB_READ(DOWNLOAD_GET_REG
, get
);
193 (DOWNLOAD_FIFO_SIZE
- DOWNLOAD_BLOCK_SIZE
))
198 if ((put
- get
) > (DOWNLOAD_FIFO_SIZE
- DOWNLOAD_BLOCK_SIZE
)) {
199 pr_err("Timeout waiting for FIFO.\n");
204 /* calculate the block size */
205 tx_size
= block_size
= min((size_t)(firmware
->size
- put
),
206 (size_t)DOWNLOAD_BLOCK_SIZE
);
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
;
215 /* send the block to sram */
216 ret
= cw1200_apb_write(priv
,
217 CW1200_APB(DOWNLOAD_FIFO_OFFSET
+
218 (put
& (DOWNLOAD_FIFO_SIZE
- 1))),
221 pr_err("Can't write firmware block @ %d!\n",
222 put
& (DOWNLOAD_FIFO_SIZE
- 1));
226 /* update the put register */
228 APB_WRITE(DOWNLOAD_PUT_REG
, put
);
229 } /* End of firmware download loop */
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
)
238 if (val32
!= DOWNLOAD_SUCCESS
) {
239 pr_err("Wait for download completion failed: 0x%.8X\n", val32
);
243 pr_info("Firmware download completed.\n");
250 release_firmware(firmware
);
260 static int config_reg_read(struct cw1200_common
*priv
, u32
*val
)
262 switch (priv
->hw_type
) {
263 case HIF_9000_SILICON_VERSATILE
: {
265 int ret
= cw1200_reg_read_16(priv
,
266 ST90TDS_CONFIG_REG_ID
,
273 case HIF_8601_VERSATILE
:
274 case HIF_8601_SILICON
:
276 cw1200_reg_read_32(priv
, ST90TDS_CONFIG_REG_ID
, val
);
282 static int config_reg_write(struct cw1200_common
*priv
, u32 val
)
284 switch (priv
->hw_type
) {
285 case HIF_9000_SILICON_VERSATILE
:
286 return cw1200_reg_write_16(priv
,
287 ST90TDS_CONFIG_REG_ID
,
289 case HIF_8601_VERSATILE
:
290 case HIF_8601_SILICON
:
292 return cw1200_reg_write_32(priv
, ST90TDS_CONFIG_REG_ID
, val
);
298 int cw1200_load_firmware(struct cw1200_common
*priv
)
304 int major_revision
= -1;
306 /* Read CONFIG Register */
307 ret
= cw1200_reg_read_32(priv
, ST90TDS_CONFIG_REG_ID
, &val32
);
309 pr_err("Can't read config register.\n");
313 if (val32
== 0 || val32
== 0xffffffff) {
314 pr_err("Bad config register value (0x%08x)\n", val32
);
319 priv
->hw_type
= cw1200_get_hw_type(val32
, &major_revision
);
320 if (priv
->hw_type
< 0) {
321 pr_err("Can't deduce hardware type.\n");
326 /* Set DPLL Reg value, and read back to confirm writes work */
327 ret
= cw1200_reg_write_32(priv
, ST90TDS_TSET_GEN_R_W_REG_ID
,
328 cw1200_dpll_from_clk(priv
->hw_refclk
));
330 pr_err("Can't write DPLL register.\n");
336 ret
= cw1200_reg_read_32(priv
,
337 ST90TDS_TSET_GEN_R_W_REG_ID
, &val32
);
339 pr_err("Can't read DPLL register.\n");
343 if (val32
!= cw1200_dpll_from_clk(priv
->hw_refclk
)) {
344 pr_err("Unable to initialise DPLL register. Wrote 0x%.8X, Read 0x%.8X.\n",
345 cw1200_dpll_from_clk(priv
->hw_refclk
), val32
);
350 /* Set wakeup bit in device */
351 ret
= cw1200_reg_read_16(priv
, ST90TDS_CONTROL_REG_ID
, &val16
);
353 pr_err("set_wakeup: can't read control register.\n");
357 ret
= cw1200_reg_write_16(priv
, ST90TDS_CONTROL_REG_ID
,
358 val16
| ST90TDS_CONT_WUP_BIT
);
360 pr_err("set_wakeup: can't write control register.\n");
364 /* Wait for wakeup */
365 for (i
= 0; i
< 300; i
+= (1 + i
/ 2)) {
366 ret
= cw1200_reg_read_16(priv
,
367 ST90TDS_CONTROL_REG_ID
, &val16
);
369 pr_err("wait_for_wakeup: can't read control register.\n");
373 if (val16
& ST90TDS_CONT_RDY_BIT
)
379 if ((val16
& ST90TDS_CONT_RDY_BIT
) == 0) {
380 pr_err("wait_for_wakeup: device is not responding.\n");
385 switch (major_revision
) {
387 /* CW1200 Hardware detection logic : Check for CUT1.1 */
388 ret
= cw1200_ahb_read_32(priv
, CW1200_CUT_ID_ADDR
, &val32
);
390 pr_err("HW detection: can't read CUT ID.\n");
395 case CW1200_CUT_11_ID_STR
:
396 pr_info("CW1x00 Cut 1.1 silicon detected.\n");
397 priv
->hw_revision
= CW1200_HW_REV_CUT11
;
400 pr_info("CW1x00 Cut 1.0 silicon detected.\n");
401 priv
->hw_revision
= CW1200_HW_REV_CUT10
;
405 /* According to ST-E, CUT<2.0 has busted BA TID0-3.
406 Just disable it entirely...
408 priv
->ba_rx_tid_mask
= 0;
409 priv
->ba_tx_tid_mask
= 0;
413 ret
= cw1200_ahb_read_32(priv
, CW1200_CUT2_ID_ADDR
, &ar1
);
415 pr_err("(1) HW detection: can't read CUT ID\n");
418 ret
= cw1200_ahb_read_32(priv
, CW1200_CUT2_ID_ADDR
+ 4, &ar2
);
420 pr_err("(2) HW detection: can't read CUT ID.\n");
424 ret
= cw1200_ahb_read_32(priv
, CW1200_CUT2_ID_ADDR
+ 8, &ar3
);
426 pr_err("(3) HW detection: can't read CUT ID.\n");
430 if (ar1
== CW1200_CUT_22_ID_STR1
&&
431 ar2
== CW1200_CUT_22_ID_STR2
&&
432 ar3
== CW1200_CUT_22_ID_STR3
) {
433 pr_info("CW1x00 Cut 2.2 silicon detected.\n");
434 priv
->hw_revision
= CW1200_HW_REV_CUT22
;
436 pr_info("CW1x00 Cut 2.0 silicon detected.\n");
437 priv
->hw_revision
= CW1200_HW_REV_CUT20
;
442 pr_info("CW1x60 silicon detected.\n");
443 priv
->hw_revision
= CW1X60_HW_REV
;
446 pr_err("Unsupported silicon major revision %d.\n",
452 /* Checking for access mode */
453 ret
= config_reg_read(priv
, &val32
);
455 pr_err("Can't read config register.\n");
459 if (!(val32
& ST90TDS_CONFIG_ACCESS_MODE_BIT
)) {
460 pr_err("Device is already in QUEUE mode!\n");
465 switch (priv
->hw_type
) {
466 case HIF_8601_SILICON
:
467 if (priv
->hw_revision
== CW1X60_HW_REV
) {
468 pr_err("Can't handle CW1160/1260 firmware load yet.\n");
472 ret
= cw1200_load_firmware_cw1200(priv
);
475 pr_err("Can't perform firmware load for hw type %d.\n",
481 pr_err("Firmware load error.\n");
485 /* Enable interrupt signalling */
486 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
487 ret
= __cw1200_irq_enable(priv
, 1);
488 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);
492 /* Configure device for MESSSAGE MODE */
493 ret
= config_reg_read(priv
, &val32
);
495 pr_err("Can't read config register.\n");
498 ret
= config_reg_write(priv
, val32
& ~ST90TDS_CONFIG_ACCESS_MODE_BIT
);
500 pr_err("Can't write config register.\n");
504 /* Unless we read the CONFIG Register we are
505 * not able to get an interrupt
508 config_reg_read(priv
, &val32
);
514 /* Disable interrupt signalling */
515 priv
->hwbus_ops
->lock(priv
->hwbus_priv
);
516 ret
= __cw1200_irq_enable(priv
, 0);
517 priv
->hwbus_ops
->unlock(priv
->hwbus_priv
);