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