8b50d44d8243ca52edf85205bff3bd357e399391
[pandora-kernel.git] / drivers / net / wireless / wl12xx / wl1251_boot.c
1 /*
2  * This file is part of wl1251
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
24 #include <linux/gpio.h>
25
26 #include "reg.h"
27 #include "wl1251_boot.h"
28 #include "wl1251_io.h"
29 #include "wl1251_spi.h"
30 #include "wl1251_event.h"
31
32 void wl1251_boot_target_enable_interrupts(struct wl1251 *wl)
33 {
34         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
35         wl1251_reg_write32(wl, HI_CFG, HI_CFG_DEF_VAL);
36 }
37
38 int wl1251_boot_soft_reset(struct wl1251 *wl)
39 {
40         unsigned long timeout;
41         u32 boot_data;
42
43         /* perform soft reset */
44         wl1251_reg_write32(wl, ACX_REG_SLV_SOFT_RESET, ACX_SLV_SOFT_RESET_BIT);
45
46         /* SOFT_RESET is self clearing */
47         timeout = jiffies + usecs_to_jiffies(SOFT_RESET_MAX_TIME);
48         while (1) {
49                 boot_data = wl1251_reg_read32(wl, ACX_REG_SLV_SOFT_RESET);
50                 wl1251_debug(DEBUG_BOOT, "soft reset bootdata 0x%x", boot_data);
51                 if ((boot_data & ACX_SLV_SOFT_RESET_BIT) == 0)
52                         break;
53
54                 if (time_after(jiffies, timeout)) {
55                         /* 1.2 check pWhalBus->uSelfClearTime if the
56                          * timeout was reached */
57                         wl1251_error("soft reset timeout");
58                         return -1;
59                 }
60
61                 udelay(SOFT_RESET_STALL_TIME);
62         }
63
64         /* disable Rx/Tx */
65         wl1251_reg_write32(wl, ENABLE, 0x0);
66
67         /* disable auto calibration on start*/
68         wl1251_reg_write32(wl, SPARE_A2, 0xffff);
69
70         return 0;
71 }
72
73 int wl1251_boot_init_seq(struct wl1251 *wl)
74 {
75         u32 scr_pad6, init_data, tmp, elp_cmd, ref_freq;
76
77         /*
78          * col #1: INTEGER_DIVIDER
79          * col #2: FRACTIONAL_DIVIDER
80          * col #3: ATTN_BB
81          * col #4: ALPHA_BB
82          * col #5: STOP_TIME_BB
83          * col #6: BB_PLL_LOOP_FILTER
84          */
85         static const u32 LUT[REF_FREQ_NUM][LUT_PARAM_NUM] = {
86
87                 {   83, 87381,  0xB, 5, 0xF00,  3}, /* REF_FREQ_19_2*/
88                 {   61, 141154, 0xB, 5, 0x1450, 2}, /* REF_FREQ_26_0*/
89                 {   41, 174763, 0xC, 6, 0x2D00, 1}, /* REF_FREQ_38_4*/
90                 {   40, 0,      0xC, 6, 0x2EE0, 1}, /* REF_FREQ_40_0*/
91                 {   47, 162280, 0xC, 6, 0x2760, 1}  /* REF_FREQ_33_6        */
92         };
93
94         /* read NVS params */
95         scr_pad6 = wl1251_reg_read32(wl, SCR_PAD6);
96         wl1251_debug(DEBUG_BOOT, "scr_pad6 0x%x", scr_pad6);
97
98         /* read ELP_CMD */
99         elp_cmd = wl1251_reg_read32(wl, ELP_CMD);
100         wl1251_debug(DEBUG_BOOT, "elp_cmd 0x%x", elp_cmd);
101
102         /* set the BB calibration time to be 300 usec (PLL_CAL_TIME) */
103         ref_freq = scr_pad6 & 0x000000FF;
104         wl1251_debug(DEBUG_BOOT, "ref_freq 0x%x", ref_freq);
105
106         wl1251_reg_write32(wl, PLL_CAL_TIME, 0x9);
107
108         /*
109          * PG 1.2: set the clock buffer time to be 210 usec (CLK_BUF_TIME)
110          */
111         wl1251_reg_write32(wl, CLK_BUF_TIME, 0x6);
112
113         /*
114          * set the clock detect feature to work in the restart wu procedure
115          * (ELP_CFG_MODE[14]) and Select the clock source type
116          * (ELP_CFG_MODE[13:12])
117          */
118         tmp = ((scr_pad6 & 0x0000FF00) << 4) | 0x00004000;
119         wl1251_reg_write32(wl, ELP_CFG_MODE, tmp);
120
121         /* PG 1.2: enable the BB PLL fix. Enable the PLL_LIMP_CLK_EN_CMD */
122         elp_cmd |= 0x00000040;
123         wl1251_reg_write32(wl, ELP_CMD, elp_cmd);
124
125         /* PG 1.2: Set the BB PLL stable time to be 1000usec
126          * (PLL_STABLE_TIME) */
127         wl1251_reg_write32(wl, CFG_PLL_SYNC_CNT, 0x20);
128
129         /* PG 1.2: read clock request time */
130         init_data = wl1251_reg_read32(wl, CLK_REQ_TIME);
131
132         /*
133          * PG 1.2: set the clock request time to be ref_clk_settling_time -
134          * 1ms = 4ms
135          */
136         if (init_data > 0x21)
137                 tmp = init_data - 0x21;
138         else
139                 tmp = 0;
140         wl1251_reg_write32(wl, CLK_REQ_TIME, tmp);
141
142         /* set BB PLL configurations in RF AFE */
143         wl1251_reg_write32(wl, 0x003058cc, 0x4B5);
144
145         /* set RF_AFE_REG_5 */
146         wl1251_reg_write32(wl, 0x003058d4, 0x50);
147
148         /* set RF_AFE_CTRL_REG_2 */
149         wl1251_reg_write32(wl, 0x00305948, 0x11c001);
150
151         /*
152          * change RF PLL and BB PLL divider for VCO clock and adjust VCO
153          * bais current(RF_AFE_REG_13)
154          */
155         wl1251_reg_write32(wl, 0x003058f4, 0x1e);
156
157         /* set BB PLL configurations */
158         tmp = LUT[ref_freq][LUT_PARAM_INTEGER_DIVIDER] | 0x00017000;
159         wl1251_reg_write32(wl, 0x00305840, tmp);
160
161         /* set fractional divider according to Appendix C-BB PLL
162          * Calculations
163          */
164         tmp = LUT[ref_freq][LUT_PARAM_FRACTIONAL_DIVIDER];
165         wl1251_reg_write32(wl, 0x00305844, tmp);
166
167         /* set the initial data for the sigma delta */
168         wl1251_reg_write32(wl, 0x00305848, 0x3039);
169
170         /*
171          * set the accumulator attenuation value, calibration loop1
172          * (alpha), calibration loop2 (beta), calibration loop3 (gamma) and
173          * the VCO gain
174          */
175         tmp = (LUT[ref_freq][LUT_PARAM_ATTN_BB] << 16) |
176                 (LUT[ref_freq][LUT_PARAM_ALPHA_BB] << 12) | 0x1;
177         wl1251_reg_write32(wl, 0x00305854, tmp);
178
179         /*
180          * set the calibration stop time after holdoff time expires and set
181          * settling time HOLD_OFF_TIME_BB
182          */
183         tmp = LUT[ref_freq][LUT_PARAM_STOP_TIME_BB] | 0x000A0000;
184         wl1251_reg_write32(wl, 0x00305858, tmp);
185
186         /*
187          * set BB PLL Loop filter capacitor3- BB_C3[2:0] and set BB PLL
188          * constant leakage current to linearize PFD to 0uA -
189          * BB_ILOOPF[7:3]
190          */
191         tmp = LUT[ref_freq][LUT_PARAM_BB_PLL_LOOP_FILTER] | 0x00000030;
192         wl1251_reg_write32(wl, 0x003058f8, tmp);
193
194         /*
195          * set regulator output voltage for n divider to
196          * 1.35-BB_REFDIV[1:0], set charge pump current- BB_CPGAIN[4:2],
197          * set BB PLL Loop filter capacitor2- BB_C2[7:5], set gain of BB
198          * PLL auto-call to normal mode- BB_CALGAIN_3DB[8]
199          */
200         wl1251_reg_write32(wl, 0x003058f0, 0x29);
201
202         /* enable restart wakeup sequence (ELP_CMD[0]) */
203         wl1251_reg_write32(wl, ELP_CMD, elp_cmd | 0x1);
204
205         /* restart sequence completed */
206         udelay(2000);
207
208         return 0;
209 }
210
211 int wl1251_boot_run_firmware(struct wl1251 *wl)
212 {
213         int loop, ret;
214         u32 chip_id, interrupt;
215
216         wl->chip.op_set_ecpu_ctrl(wl, ECPU_CONTROL_HALT);
217
218         chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
219
220         wl1251_debug(DEBUG_BOOT, "chip id after firmware boot: 0x%x", chip_id);
221
222         if (chip_id != wl->chip.id) {
223                 wl1251_error("chip id doesn't match after firmware boot");
224                 return -EIO;
225         }
226
227         /* wait for init to complete */
228         loop = 0;
229         while (loop++ < INIT_LOOP) {
230                 udelay(INIT_LOOP_DELAY);
231                 interrupt = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
232
233                 if (interrupt == 0xffffffff) {
234                         wl1251_error("error reading hardware complete "
235                                      "init indication");
236                         return -EIO;
237                 }
238                 /* check that ACX_INTR_INIT_COMPLETE is enabled */
239                 else if (interrupt & wl->chip.intr_init_complete) {
240                         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
241                                            wl->chip.intr_init_complete);
242                         break;
243                 }
244         }
245
246         if (loop >= INIT_LOOP) {
247                 wl1251_error("timeout waiting for the hardware to "
248                              "complete initialization");
249                 return -EIO;
250         }
251
252         /* get hardware config command mail box */
253         wl->cmd_box_addr = wl1251_reg_read32(wl, REG_COMMAND_MAILBOX_PTR);
254
255         /* get hardware config event mail box */
256         wl->event_box_addr = wl1251_reg_read32(wl, REG_EVENT_MAILBOX_PTR);
257
258         /* set the working partition to its "running" mode offset */
259         wl1251_set_partition(wl,
260                              wl->chip.p_table[PART_WORK].mem.start,
261                              wl->chip.p_table[PART_WORK].mem.size,
262                              wl->chip.p_table[PART_WORK].reg.start,
263                              wl->chip.p_table[PART_WORK].reg.size);
264
265         wl1251_debug(DEBUG_MAILBOX, "cmd_box_addr 0x%x event_box_addr 0x%x",
266                      wl->cmd_box_addr, wl->event_box_addr);
267
268         wl->chip.op_fw_version(wl);
269
270         /*
271          * in case of full asynchronous mode the firmware event must be
272          * ready to receive event from the command mailbox
273          */
274
275         /* enable gpio interrupts */
276         wl1251_enable_interrupts(wl);
277
278         wl->chip.op_target_enable_interrupts(wl);
279
280         /* unmask all mbox events  */
281         wl->event_mask = 0xffffffff;
282
283         ret = wl1251_event_unmask(wl);
284         if (ret < 0) {
285                 wl1251_error("EVENT mask setting failed");
286                 return ret;
287         }
288
289         wl1251_event_mbox_config(wl);
290
291         /* firmware startup completed */
292         return 0;
293 }