linux/drivers/staging/rtl8712/hal_init.c
<<
>>
Prefs
   1/******************************************************************************
   2 * hal_init.c
   3 *
   4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
   5 * Linux device driver for RTL8192SU
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms of version 2 of the GNU General Public License as
   9 * published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * Modifications for inclusion into the Linux staging tree are
  17 * Copyright(c) 2010 Larry Finger. All rights reserved.
  18 *
  19 * Contact information:
  20 * WLAN FAE <wlanfae@realtek.com>.
  21 * Larry Finger <Larry.Finger@lwfinger.net>
  22 *
  23 ******************************************************************************/
  24
  25#define _HAL_INIT_C_
  26
  27#include <linux/usb.h>
  28#include <linux/device.h>
  29#include <linux/usb/ch9.h>
  30#include <linux/firmware.h>
  31#include <linux/module.h>
  32
  33#include "osdep_service.h"
  34#include "drv_types.h"
  35#include "usb_osintf.h"
  36
  37#define FWBUFF_ALIGN_SZ 512
  38#define MAX_DUMP_FWSZ   49152 /*default = 49152 (48k)*/
  39
  40static void rtl871x_load_fw_cb(const struct firmware *firmware, void *context)
  41{
  42        struct _adapter *padapter = context;
  43
  44        complete(&padapter->rtl8712_fw_ready);
  45        if (!firmware) {
  46                struct usb_device *udev = padapter->dvobjpriv.pusbdev;
  47                struct usb_interface *pusb_intf = padapter->pusb_intf;
  48
  49                dev_err(&udev->dev, "r8712u: Firmware request failed\n");
  50                usb_put_dev(udev);
  51                usb_set_intfdata(pusb_intf, NULL);
  52                return;
  53        }
  54        padapter->fw = firmware;
  55        /* firmware available - start netdev */
  56        register_netdev(padapter->pnetdev);
  57}
  58
  59static const char firmware_file[] = "rtlwifi/rtl8712u.bin";
  60
  61int rtl871x_load_fw(struct _adapter *padapter)
  62{
  63        struct device *dev = &padapter->dvobjpriv.pusbdev->dev;
  64        int rc;
  65
  66        init_completion(&padapter->rtl8712_fw_ready);
  67        dev_info(dev, "r8712u: Loading firmware from \"%s\"\n", firmware_file);
  68        rc = request_firmware_nowait(THIS_MODULE, 1, firmware_file, dev,
  69                                     GFP_KERNEL, padapter, rtl871x_load_fw_cb);
  70        if (rc)
  71                dev_err(dev, "r8712u: Firmware request error %d\n", rc);
  72        return rc;
  73}
  74MODULE_FIRMWARE("rtlwifi/rtl8712u.bin");
  75
  76static u32 rtl871x_open_fw(struct _adapter *padapter, const u8 **ppmappedfw)
  77{
  78        const struct firmware **praw = &padapter->fw;
  79
  80        if (padapter->fw->size > 200000) {
  81                dev_err(&padapter->pnetdev->dev, "r8172u: Badfw->size of %d\n",
  82                        (int)padapter->fw->size);
  83                return 0;
  84        }
  85        *ppmappedfw = (*praw)->data;
  86        return (*praw)->size;
  87}
  88
  89static void fill_fwpriv(struct _adapter *padapter, struct fw_priv *pfwpriv)
  90{
  91        struct dvobj_priv *pdvobj = &padapter->dvobjpriv;
  92        struct registry_priv *pregpriv = &padapter->registrypriv;
  93
  94        memset(pfwpriv, 0, sizeof(struct fw_priv));
  95        /* todo: check if needs endian conversion */
  96        pfwpriv->hci_sel =  RTL8712_HCI_TYPE_72USB;
  97        pfwpriv->usb_ep_num = (u8)pdvobj->nr_endpoint;
  98        pfwpriv->bw_40MHz_en = pregpriv->cbw40_enable;
  99        switch (pregpriv->rf_config) {
 100        case RTL8712_RF_1T1R:
 101                pfwpriv->rf_config = RTL8712_RFC_1T1R;
 102                break;
 103        case RTL8712_RF_2T2R:
 104                pfwpriv->rf_config = RTL8712_RFC_2T2R;
 105                break;
 106        case RTL8712_RF_1T2R:
 107        default:
 108                pfwpriv->rf_config = RTL8712_RFC_1T2R;
 109        }
 110        pfwpriv->mp_mode = (pregpriv->mp_mode == 1) ? 1 : 0;
 111        pfwpriv->vcsType = pregpriv->vrtl_carrier_sense; /* 0:off 1:on 2:auto */
 112        pfwpriv->vcsMode = pregpriv->vcs_type; /* 1:RTS/CTS 2:CTS to self */
 113        /* default enable turboMode */
 114        pfwpriv->turboMode = ((pregpriv->wifi_test == 1) ? 0 : 1);
 115        pfwpriv->lowPowerMode = pregpriv->low_power;
 116}
 117
 118static void update_fwhdr(struct fw_hdr  *pfwhdr, const u8 *pmappedfw)
 119{
 120        pfwhdr->signature = le16_to_cpu(*(__le16 *)pmappedfw);
 121        pfwhdr->version = le16_to_cpu(*(__le16 *)(pmappedfw + 2));
 122        /* define the size of boot loader */
 123        pfwhdr->dmem_size = le32_to_cpu(*(__le32 *)(pmappedfw + 4));
 124        /* define the size of FW in IMEM */
 125        pfwhdr->img_IMEM_size = le32_to_cpu(*(__le32 *)(pmappedfw + 8));
 126        /* define the size of FW in SRAM */
 127        pfwhdr->img_SRAM_size = le32_to_cpu(*(__le32 *)(pmappedfw + 12));
 128        /* define the size of DMEM variable */
 129        pfwhdr->fw_priv_sz = le32_to_cpu(*(__le32 *)(pmappedfw + 16));
 130}
 131
 132static u8 chk_fwhdr(struct fw_hdr *pfwhdr, u32 ulfilelength)
 133{
 134        u32     fwhdrsz, fw_sz;
 135
 136        /* check signature */
 137        if ((pfwhdr->signature != 0x8712) && (pfwhdr->signature != 0x8192))
 138                return _FAIL;
 139        /* check fw_priv_sze & sizeof(struct fw_priv) */
 140        if (pfwhdr->fw_priv_sz != sizeof(struct fw_priv))
 141                return _FAIL;
 142        /* check fw_sz & image_fw_sz */
 143        fwhdrsz = FIELD_OFFSET(struct fw_hdr, fwpriv) + pfwhdr->fw_priv_sz;
 144        fw_sz =  fwhdrsz + pfwhdr->img_IMEM_size + pfwhdr->img_SRAM_size +
 145                 pfwhdr->dmem_size;
 146        if (fw_sz != ulfilelength)
 147                return _FAIL;
 148        return _SUCCESS;
 149}
 150
 151static u8 rtl8712_dl_fw(struct _adapter *padapter)
 152{
 153        sint i;
 154        u8 tmp8, tmp8_a;
 155        u16 tmp16;
 156        u32 maxlen = 0; /* for compare usage */
 157        uint dump_imem_sz, imem_sz, dump_emem_sz, emem_sz; /* max = 49152; */
 158        struct fw_hdr fwhdr;
 159        u32 ulfilelength;       /* FW file size */
 160        const u8 *pmappedfw = NULL;
 161        u8 *ptmpchar = NULL, *ppayload, *ptr;
 162        struct tx_desc *ptx_desc;
 163        u32 txdscp_sz = sizeof(struct tx_desc);
 164        u8 ret = _FAIL;
 165
 166        ulfilelength = rtl871x_open_fw(padapter, &pmappedfw);
 167        if (pmappedfw && (ulfilelength > 0)) {
 168                update_fwhdr(&fwhdr, pmappedfw);
 169                if (chk_fwhdr(&fwhdr, ulfilelength) == _FAIL)
 170                        return ret;
 171                fill_fwpriv(padapter, &fwhdr.fwpriv);
 172                /* firmware check ok */
 173                maxlen = (fwhdr.img_IMEM_size > fwhdr.img_SRAM_size) ?
 174                          fwhdr.img_IMEM_size : fwhdr.img_SRAM_size;
 175                maxlen += txdscp_sz;
 176                ptmpchar = kmalloc(maxlen + FWBUFF_ALIGN_SZ, GFP_KERNEL);
 177                if (!ptmpchar)
 178                        return ret;
 179
 180                ptx_desc = (struct tx_desc *)(ptmpchar + FWBUFF_ALIGN_SZ -
 181                            ((addr_t)(ptmpchar) & (FWBUFF_ALIGN_SZ - 1)));
 182                ppayload = (u8 *)(ptx_desc) + txdscp_sz;
 183                ptr = (u8 *)pmappedfw + FIELD_OFFSET(struct fw_hdr, fwpriv) +
 184                      fwhdr.fw_priv_sz;
 185                /* Download FirmWare */
 186                /* 1. determine IMEM code size and Load IMEM Code Section */
 187                imem_sz = fwhdr.img_IMEM_size;
 188                do {
 189                        memset(ptx_desc, 0, TXDESC_SIZE);
 190                        if (imem_sz >  MAX_DUMP_FWSZ/*49152*/) {
 191                                dump_imem_sz = MAX_DUMP_FWSZ;
 192                        } else {
 193                                dump_imem_sz = imem_sz;
 194                                ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
 195                        }
 196                        ptx_desc->txdw0 |= cpu_to_le32(dump_imem_sz &
 197                                                       0x0000ffff);
 198                        memcpy(ppayload, ptr, dump_imem_sz);
 199                        r8712_write_mem(padapter, RTL8712_DMA_VOQ,
 200                                        dump_imem_sz + TXDESC_SIZE,
 201                                        (u8 *)ptx_desc);
 202                        ptr += dump_imem_sz;
 203                        imem_sz -= dump_imem_sz;
 204                } while (imem_sz > 0);
 205                i = 10;
 206                tmp16 = r8712_read16(padapter, TCR);
 207                while (((tmp16 & _IMEM_CODE_DONE) == 0) && (i > 0)) {
 208                        usleep_range(10, 1000);
 209                        tmp16 = r8712_read16(padapter, TCR);
 210                        i--;
 211                }
 212                if (i == 0 || (tmp16 & _IMEM_CHK_RPT) == 0)
 213                        goto exit_fail;
 214
 215                /* 2.Download EMEM code size and Load EMEM Code Section */
 216                emem_sz = fwhdr.img_SRAM_size;
 217                do {
 218                        memset(ptx_desc, 0, TXDESC_SIZE);
 219                        if (emem_sz >  MAX_DUMP_FWSZ) /* max=48k */
 220                                dump_emem_sz = MAX_DUMP_FWSZ;
 221                        else {
 222                                dump_emem_sz = emem_sz;
 223                                ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
 224                        }
 225                        ptx_desc->txdw0 |= cpu_to_le32(dump_emem_sz &
 226                                                       0x0000ffff);
 227                        memcpy(ppayload, ptr, dump_emem_sz);
 228                        r8712_write_mem(padapter, RTL8712_DMA_VOQ,
 229                                        dump_emem_sz + TXDESC_SIZE,
 230                                        (u8 *)ptx_desc);
 231                        ptr += dump_emem_sz;
 232                        emem_sz -= dump_emem_sz;
 233                } while (emem_sz > 0);
 234                i = 5;
 235                tmp16 = r8712_read16(padapter, TCR);
 236                while (((tmp16 & _EMEM_CODE_DONE) == 0) && (i > 0)) {
 237                        usleep_range(10, 1000);
 238                        tmp16 = r8712_read16(padapter, TCR);
 239                        i--;
 240                }
 241                if (i == 0 || (tmp16 & _EMEM_CHK_RPT) == 0)
 242                        goto exit_fail;
 243
 244                /* 3.Enable CPU */
 245                tmp8 = r8712_read8(padapter, SYS_CLKR);
 246                r8712_write8(padapter, SYS_CLKR, tmp8 | BIT(2));
 247                tmp8_a = r8712_read8(padapter, SYS_CLKR);
 248                if (tmp8_a != (tmp8 | BIT(2)))
 249                        goto exit_fail;
 250
 251                tmp8 = r8712_read8(padapter, SYS_FUNC_EN + 1);
 252                r8712_write8(padapter, SYS_FUNC_EN + 1, tmp8 | BIT(2));
 253                tmp8_a = r8712_read8(padapter, SYS_FUNC_EN + 1);
 254                if (tmp8_a != (tmp8 | BIT(2)))
 255                        goto exit_fail;
 256
 257                r8712_read32(padapter, TCR);
 258
 259                /* 4.polling IMEM Ready */
 260                i = 100;
 261                tmp16 = r8712_read16(padapter, TCR);
 262                while (((tmp16 & _IMEM_RDY) == 0) && (i > 0)) {
 263                        msleep(20);
 264                        tmp16 = r8712_read16(padapter, TCR);
 265                        i--;
 266                }
 267                if (i == 0) {
 268                        r8712_write16(padapter, 0x10250348, 0xc000);
 269                        r8712_write16(padapter, 0x10250348, 0xc001);
 270                        r8712_write16(padapter, 0x10250348, 0x2000);
 271                        r8712_write16(padapter, 0x10250348, 0x2001);
 272                        r8712_write16(padapter, 0x10250348, 0x2002);
 273                        r8712_write16(padapter, 0x10250348, 0x2003);
 274                        goto exit_fail;
 275                }
 276                /* 5.Download DMEM code size and Load EMEM Code Section */
 277                memset(ptx_desc, 0, TXDESC_SIZE);
 278                ptx_desc->txdw0 |= cpu_to_le32(fwhdr.fw_priv_sz & 0x0000ffff);
 279                ptx_desc->txdw0 |= cpu_to_le32(BIT(28));
 280                memcpy(ppayload, &fwhdr.fwpriv, fwhdr.fw_priv_sz);
 281                r8712_write_mem(padapter, RTL8712_DMA_VOQ,
 282                                fwhdr.fw_priv_sz + TXDESC_SIZE, (u8 *)ptx_desc);
 283
 284                /* polling dmem code done */
 285                i = 100;
 286                tmp16 = r8712_read16(padapter, TCR);
 287                while (((tmp16 & _DMEM_CODE_DONE) == 0) && (i > 0)) {
 288                        msleep(20);
 289                        tmp16 = r8712_read16(padapter, TCR);
 290                        i--;
 291                }
 292                if (i == 0)
 293                        goto exit_fail;
 294
 295                tmp8 = r8712_read8(padapter, 0x1025000A);
 296                if (tmp8 & BIT(4)) /* When boot from EEPROM,
 297                                    * & FW need more time to read EEPROM
 298                                    */
 299                        i = 60;
 300                else                    /* boot from EFUSE */
 301                        i = 30;
 302                tmp16 = r8712_read16(padapter, TCR);
 303                while (((tmp16 & _FWRDY) == 0) && (i > 0)) {
 304                        msleep(100);
 305                        tmp16 = r8712_read16(padapter, TCR);
 306                        i--;
 307                }
 308                if (i == 0)
 309                        goto exit_fail;
 310        } else {
 311                goto exit_fail;
 312        }
 313        ret = _SUCCESS;
 314
 315exit_fail:
 316        kfree(ptmpchar);
 317        return ret;
 318}
 319
 320uint rtl8712_hal_init(struct _adapter *padapter)
 321{
 322        u32 val32;
 323        int i;
 324
 325        /* r8712 firmware download */
 326        if (rtl8712_dl_fw(padapter) != _SUCCESS)
 327                return _FAIL;
 328
 329        netdev_info(padapter->pnetdev, "1 RCR=0x%x\n",
 330                    r8712_read32(padapter, RCR));
 331        val32 = r8712_read32(padapter, RCR);
 332        r8712_write32(padapter, RCR, (val32 | BIT(26))); /* Enable RX TCP
 333                                                          * Checksum offload
 334                                                          */
 335        netdev_info(padapter->pnetdev, "2 RCR=0x%x\n",
 336                    r8712_read32(padapter, RCR));
 337        val32 = r8712_read32(padapter, RCR);
 338        r8712_write32(padapter, RCR, (val32 | BIT(25))); /* Append PHY status */
 339        val32 = 0;
 340        val32 = r8712_read32(padapter, 0x10250040);
 341        r8712_write32(padapter,  0x10250040, (val32 & 0x00FFFFFF));
 342        /* for usb rx aggregation */
 343        r8712_write8(padapter, 0x102500B5, r8712_read8(padapter, 0x102500B5) |
 344               BIT(0)); /* page = 128bytes */
 345        r8712_write8(padapter, 0x102500BD, r8712_read8(padapter, 0x102500BD) |
 346               BIT(7)); /* enable usb rx aggregation */
 347        r8712_write8(padapter, 0x102500D9, 1); /* TH=1 => means that invalidate
 348                                                *  usb rx aggregation
 349                                                */
 350        r8712_write8(padapter, 0x1025FE5B, 0x04); /* 1.7ms/4 */
 351        /* Fix the RX FIFO issue(USB error) */
 352        r8712_write8(padapter, 0x1025fe5C, r8712_read8(padapter, 0x1025fe5C)
 353                     | BIT(7));
 354        for (i = 0; i < 6; i++)
 355                padapter->eeprompriv.mac_addr[i] = r8712_read8(padapter,
 356                                                               MACID + i);
 357        return _SUCCESS;
 358}
 359
 360uint rtl8712_hal_deinit(struct _adapter *padapter)
 361{
 362        r8712_write8(padapter, RF_CTRL, 0x00);
 363        /* Turn off BB */
 364        msleep(20);
 365        /* Turn off MAC */
 366        r8712_write8(padapter, SYS_CLKR + 1, 0x38); /* Switch Control Path */
 367        r8712_write8(padapter, SYS_FUNC_EN + 1, 0x70);
 368        r8712_write8(padapter, PMC_FSM, 0x06);  /* Enable Loader Data Keep */
 369        r8712_write8(padapter, SYS_ISO_CTRL, 0xF9); /* Isolation signals from
 370                                                     * CORE, PLL
 371                                                     */
 372        r8712_write8(padapter, SYS_ISO_CTRL + 1, 0xe8); /* Enable EFUSE 1.2V */
 373        r8712_write8(padapter, AFE_PLL_CTRL, 0x00); /* Disable AFE PLL. */
 374        r8712_write8(padapter, LDOA15_CTRL, 0x54);  /* Disable A15V */
 375        r8712_write8(padapter, SYS_FUNC_EN + 1, 0x50); /* Disable E-Fuse 1.2V */
 376        r8712_write8(padapter, LDOV12D_CTRL, 0x24); /* Disable LDO12(for CE) */
 377        r8712_write8(padapter, AFE_MISC, 0x30); /* Disable AFE BG&MB */
 378        /* Option for Disable 1.6V LDO. */
 379        r8712_write8(padapter, SPS0_CTRL, 0x56); /* Disable 1.6V LDO */
 380        r8712_write8(padapter, SPS0_CTRL + 1, 0x43);  /* Set SW PFM */
 381        return _SUCCESS;
 382}
 383
 384uint rtl871x_hal_init(struct _adapter *padapter)
 385{
 386        padapter->hw_init_completed = false;
 387        if (!padapter->halpriv.hal_bus_init)
 388                return _FAIL;
 389        if (padapter->halpriv.hal_bus_init(padapter) != _SUCCESS)
 390                return _FAIL;
 391        if (rtl8712_hal_init(padapter) == _SUCCESS) {
 392                padapter->hw_init_completed = true;
 393        } else {
 394                padapter->hw_init_completed = false;
 395                return _FAIL;
 396        }
 397        return _SUCCESS;
 398}
 399