165371a3fSMartin Blumenstingl // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
265371a3fSMartin Blumenstingl /* Copyright (C) 2021 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
365371a3fSMartin Blumenstingl * Copyright (C) 2021 Jernej Skrabec <jernej.skrabec@gmail.com>
465371a3fSMartin Blumenstingl *
565371a3fSMartin Blumenstingl * Based on rtw88/pci.c:
665371a3fSMartin Blumenstingl * Copyright(c) 2018-2019 Realtek Corporation
765371a3fSMartin Blumenstingl */
865371a3fSMartin Blumenstingl
965371a3fSMartin Blumenstingl #include <linux/module.h>
1065371a3fSMartin Blumenstingl #include <linux/mmc/host.h>
1165371a3fSMartin Blumenstingl #include <linux/mmc/sdio_func.h>
1265371a3fSMartin Blumenstingl #include "main.h"
1365371a3fSMartin Blumenstingl #include "debug.h"
1465371a3fSMartin Blumenstingl #include "fw.h"
1565371a3fSMartin Blumenstingl #include "ps.h"
1665371a3fSMartin Blumenstingl #include "reg.h"
1765371a3fSMartin Blumenstingl #include "rx.h"
1865371a3fSMartin Blumenstingl #include "sdio.h"
1965371a3fSMartin Blumenstingl #include "tx.h"
2065371a3fSMartin Blumenstingl
2165371a3fSMartin Blumenstingl #define RTW_SDIO_INDIRECT_RW_RETRIES 50
2265371a3fSMartin Blumenstingl
rtw_sdio_is_bus_addr(u32 addr)2365371a3fSMartin Blumenstingl static bool rtw_sdio_is_bus_addr(u32 addr)
2465371a3fSMartin Blumenstingl {
2565371a3fSMartin Blumenstingl return !!(addr & RTW_SDIO_BUS_MSK);
2665371a3fSMartin Blumenstingl }
2765371a3fSMartin Blumenstingl
rtw_sdio_bus_claim_needed(struct rtw_sdio * rtwsdio)2865371a3fSMartin Blumenstingl static bool rtw_sdio_bus_claim_needed(struct rtw_sdio *rtwsdio)
2965371a3fSMartin Blumenstingl {
3065371a3fSMartin Blumenstingl return !rtwsdio->irq_thread ||
3165371a3fSMartin Blumenstingl rtwsdio->irq_thread != current;
3265371a3fSMartin Blumenstingl }
3365371a3fSMartin Blumenstingl
rtw_sdio_to_bus_offset(struct rtw_dev * rtwdev,u32 addr)3465371a3fSMartin Blumenstingl static u32 rtw_sdio_to_bus_offset(struct rtw_dev *rtwdev, u32 addr)
3565371a3fSMartin Blumenstingl {
3665371a3fSMartin Blumenstingl switch (addr & RTW_SDIO_BUS_MSK) {
3765371a3fSMartin Blumenstingl case WLAN_IOREG_OFFSET:
3865371a3fSMartin Blumenstingl addr &= WLAN_IOREG_REG_MSK;
3965371a3fSMartin Blumenstingl addr |= FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
4065371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_MAC_REG);
4165371a3fSMartin Blumenstingl break;
4265371a3fSMartin Blumenstingl case SDIO_LOCAL_OFFSET:
4365371a3fSMartin Blumenstingl addr &= SDIO_LOCAL_REG_MSK;
4465371a3fSMartin Blumenstingl addr |= FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
4565371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_SDIO_REG);
4665371a3fSMartin Blumenstingl break;
4765371a3fSMartin Blumenstingl default:
4865371a3fSMartin Blumenstingl rtw_warn(rtwdev, "Cannot convert addr 0x%08x to bus offset",
4965371a3fSMartin Blumenstingl addr);
5065371a3fSMartin Blumenstingl }
5165371a3fSMartin Blumenstingl
5265371a3fSMartin Blumenstingl return addr;
5365371a3fSMartin Blumenstingl }
5465371a3fSMartin Blumenstingl
rtw_sdio_use_memcpy_io(struct rtw_dev * rtwdev,u32 addr,u8 alignment)5565371a3fSMartin Blumenstingl static bool rtw_sdio_use_memcpy_io(struct rtw_dev *rtwdev, u32 addr,
5665371a3fSMartin Blumenstingl u8 alignment)
5765371a3fSMartin Blumenstingl {
5865371a3fSMartin Blumenstingl return IS_ALIGNED(addr, alignment) &&
5965371a3fSMartin Blumenstingl test_bit(RTW_FLAG_POWERON, rtwdev->flags);
6065371a3fSMartin Blumenstingl }
6165371a3fSMartin Blumenstingl
rtw_sdio_writel(struct rtw_dev * rtwdev,u32 val,u32 addr,int * err_ret)6265371a3fSMartin Blumenstingl static void rtw_sdio_writel(struct rtw_dev *rtwdev, u32 val, u32 addr,
6365371a3fSMartin Blumenstingl int *err_ret)
6465371a3fSMartin Blumenstingl {
6565371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
6665371a3fSMartin Blumenstingl u8 buf[4];
6765371a3fSMartin Blumenstingl int i;
6865371a3fSMartin Blumenstingl
6965371a3fSMartin Blumenstingl if (rtw_sdio_use_memcpy_io(rtwdev, addr, 4)) {
7065371a3fSMartin Blumenstingl sdio_writel(rtwsdio->sdio_func, val, addr, err_ret);
7165371a3fSMartin Blumenstingl return;
7265371a3fSMartin Blumenstingl }
7365371a3fSMartin Blumenstingl
7465371a3fSMartin Blumenstingl *(__le32 *)buf = cpu_to_le32(val);
7565371a3fSMartin Blumenstingl
7665371a3fSMartin Blumenstingl for (i = 0; i < 4; i++) {
7765371a3fSMartin Blumenstingl sdio_writeb(rtwsdio->sdio_func, buf[i], addr + i, err_ret);
7865371a3fSMartin Blumenstingl if (*err_ret)
7965371a3fSMartin Blumenstingl return;
8065371a3fSMartin Blumenstingl }
8165371a3fSMartin Blumenstingl }
8265371a3fSMartin Blumenstingl
rtw_sdio_writew(struct rtw_dev * rtwdev,u16 val,u32 addr,int * err_ret)8365371a3fSMartin Blumenstingl static void rtw_sdio_writew(struct rtw_dev *rtwdev, u16 val, u32 addr,
8465371a3fSMartin Blumenstingl int *err_ret)
8565371a3fSMartin Blumenstingl {
8665371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
8765371a3fSMartin Blumenstingl u8 buf[2];
8865371a3fSMartin Blumenstingl int i;
8965371a3fSMartin Blumenstingl
9065371a3fSMartin Blumenstingl *(__le16 *)buf = cpu_to_le16(val);
9165371a3fSMartin Blumenstingl
9265371a3fSMartin Blumenstingl for (i = 0; i < 2; i++) {
9365371a3fSMartin Blumenstingl sdio_writeb(rtwsdio->sdio_func, buf[i], addr + i, err_ret);
9465371a3fSMartin Blumenstingl if (*err_ret)
9565371a3fSMartin Blumenstingl return;
9665371a3fSMartin Blumenstingl }
9765371a3fSMartin Blumenstingl }
9865371a3fSMartin Blumenstingl
rtw_sdio_readl(struct rtw_dev * rtwdev,u32 addr,int * err_ret)9965371a3fSMartin Blumenstingl static u32 rtw_sdio_readl(struct rtw_dev *rtwdev, u32 addr, int *err_ret)
10065371a3fSMartin Blumenstingl {
10165371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
10265371a3fSMartin Blumenstingl u8 buf[4];
10365371a3fSMartin Blumenstingl int i;
10465371a3fSMartin Blumenstingl
10565371a3fSMartin Blumenstingl if (rtw_sdio_use_memcpy_io(rtwdev, addr, 4))
10665371a3fSMartin Blumenstingl return sdio_readl(rtwsdio->sdio_func, addr, err_ret);
10765371a3fSMartin Blumenstingl
10865371a3fSMartin Blumenstingl for (i = 0; i < 4; i++) {
10965371a3fSMartin Blumenstingl buf[i] = sdio_readb(rtwsdio->sdio_func, addr + i, err_ret);
11065371a3fSMartin Blumenstingl if (*err_ret)
11165371a3fSMartin Blumenstingl return 0;
11265371a3fSMartin Blumenstingl }
11365371a3fSMartin Blumenstingl
11465371a3fSMartin Blumenstingl return le32_to_cpu(*(__le32 *)buf);
11565371a3fSMartin Blumenstingl }
11665371a3fSMartin Blumenstingl
rtw_sdio_readw(struct rtw_dev * rtwdev,u32 addr,int * err_ret)11765371a3fSMartin Blumenstingl static u16 rtw_sdio_readw(struct rtw_dev *rtwdev, u32 addr, int *err_ret)
11865371a3fSMartin Blumenstingl {
11965371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
12065371a3fSMartin Blumenstingl u8 buf[2];
12165371a3fSMartin Blumenstingl int i;
12265371a3fSMartin Blumenstingl
12365371a3fSMartin Blumenstingl for (i = 0; i < 2; i++) {
12465371a3fSMartin Blumenstingl buf[i] = sdio_readb(rtwsdio->sdio_func, addr + i, err_ret);
12565371a3fSMartin Blumenstingl if (*err_ret)
12665371a3fSMartin Blumenstingl return 0;
12765371a3fSMartin Blumenstingl }
12865371a3fSMartin Blumenstingl
12965371a3fSMartin Blumenstingl return le16_to_cpu(*(__le16 *)buf);
13065371a3fSMartin Blumenstingl }
13165371a3fSMartin Blumenstingl
rtw_sdio_to_io_address(struct rtw_dev * rtwdev,u32 addr,bool direct)13265371a3fSMartin Blumenstingl static u32 rtw_sdio_to_io_address(struct rtw_dev *rtwdev, u32 addr,
13365371a3fSMartin Blumenstingl bool direct)
13465371a3fSMartin Blumenstingl {
13565371a3fSMartin Blumenstingl if (!direct)
13665371a3fSMartin Blumenstingl return addr;
13765371a3fSMartin Blumenstingl
13865371a3fSMartin Blumenstingl if (!rtw_sdio_is_bus_addr(addr))
13965371a3fSMartin Blumenstingl addr |= WLAN_IOREG_OFFSET;
14065371a3fSMartin Blumenstingl
14165371a3fSMartin Blumenstingl return rtw_sdio_to_bus_offset(rtwdev, addr);
14265371a3fSMartin Blumenstingl }
14365371a3fSMartin Blumenstingl
rtw_sdio_use_direct_io(struct rtw_dev * rtwdev,u32 addr)14465371a3fSMartin Blumenstingl static bool rtw_sdio_use_direct_io(struct rtw_dev *rtwdev, u32 addr)
14565371a3fSMartin Blumenstingl {
14665371a3fSMartin Blumenstingl return !rtw_sdio_is_sdio30_supported(rtwdev) ||
14765371a3fSMartin Blumenstingl rtw_sdio_is_bus_addr(addr);
14865371a3fSMartin Blumenstingl }
14965371a3fSMartin Blumenstingl
rtw_sdio_indirect_reg_cfg(struct rtw_dev * rtwdev,u32 addr,u32 cfg)15065371a3fSMartin Blumenstingl static int rtw_sdio_indirect_reg_cfg(struct rtw_dev *rtwdev, u32 addr, u32 cfg)
15165371a3fSMartin Blumenstingl {
15265371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
15365371a3fSMartin Blumenstingl unsigned int retry;
15465371a3fSMartin Blumenstingl u32 reg_cfg;
15565371a3fSMartin Blumenstingl int ret;
15665371a3fSMartin Blumenstingl u8 tmp;
15765371a3fSMartin Blumenstingl
15865371a3fSMartin Blumenstingl reg_cfg = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_CFG);
15965371a3fSMartin Blumenstingl
16065371a3fSMartin Blumenstingl rtw_sdio_writel(rtwdev, addr | cfg | BIT_SDIO_INDIRECT_REG_CFG_UNK20,
16165371a3fSMartin Blumenstingl reg_cfg, &ret);
16265371a3fSMartin Blumenstingl if (ret)
16365371a3fSMartin Blumenstingl return ret;
16465371a3fSMartin Blumenstingl
16565371a3fSMartin Blumenstingl for (retry = 0; retry < RTW_SDIO_INDIRECT_RW_RETRIES; retry++) {
16665371a3fSMartin Blumenstingl tmp = sdio_readb(rtwsdio->sdio_func, reg_cfg + 2, &ret);
16765371a3fSMartin Blumenstingl if (!ret && (tmp & BIT(4)))
16865371a3fSMartin Blumenstingl return 0;
16965371a3fSMartin Blumenstingl }
17065371a3fSMartin Blumenstingl
17165371a3fSMartin Blumenstingl return -ETIMEDOUT;
17265371a3fSMartin Blumenstingl }
17365371a3fSMartin Blumenstingl
rtw_sdio_indirect_read8(struct rtw_dev * rtwdev,u32 addr,int * err_ret)17465371a3fSMartin Blumenstingl static u8 rtw_sdio_indirect_read8(struct rtw_dev *rtwdev, u32 addr,
17565371a3fSMartin Blumenstingl int *err_ret)
17665371a3fSMartin Blumenstingl {
17765371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
17865371a3fSMartin Blumenstingl u32 reg_data;
17965371a3fSMartin Blumenstingl
18065371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
18165371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_READ);
18265371a3fSMartin Blumenstingl if (*err_ret)
18365371a3fSMartin Blumenstingl return 0;
18465371a3fSMartin Blumenstingl
18565371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
18665371a3fSMartin Blumenstingl return sdio_readb(rtwsdio->sdio_func, reg_data, err_ret);
18765371a3fSMartin Blumenstingl }
18865371a3fSMartin Blumenstingl
rtw_sdio_indirect_read_bytes(struct rtw_dev * rtwdev,u32 addr,u8 * buf,int count)18965371a3fSMartin Blumenstingl static int rtw_sdio_indirect_read_bytes(struct rtw_dev *rtwdev, u32 addr,
19065371a3fSMartin Blumenstingl u8 *buf, int count)
19165371a3fSMartin Blumenstingl {
19265371a3fSMartin Blumenstingl int i, ret = 0;
19365371a3fSMartin Blumenstingl
19465371a3fSMartin Blumenstingl for (i = 0; i < count; i++) {
19565371a3fSMartin Blumenstingl buf[i] = rtw_sdio_indirect_read8(rtwdev, addr + i, &ret);
19665371a3fSMartin Blumenstingl if (ret)
19765371a3fSMartin Blumenstingl break;
19865371a3fSMartin Blumenstingl }
19965371a3fSMartin Blumenstingl
20065371a3fSMartin Blumenstingl return ret;
20165371a3fSMartin Blumenstingl }
20265371a3fSMartin Blumenstingl
rtw_sdio_indirect_read16(struct rtw_dev * rtwdev,u32 addr,int * err_ret)20365371a3fSMartin Blumenstingl static u16 rtw_sdio_indirect_read16(struct rtw_dev *rtwdev, u32 addr,
20465371a3fSMartin Blumenstingl int *err_ret)
20565371a3fSMartin Blumenstingl {
20665371a3fSMartin Blumenstingl u32 reg_data;
20765371a3fSMartin Blumenstingl u8 buf[2];
20865371a3fSMartin Blumenstingl
20965371a3fSMartin Blumenstingl if (!IS_ALIGNED(addr, 2)) {
21065371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_read_bytes(rtwdev, addr, buf, 2);
21165371a3fSMartin Blumenstingl if (*err_ret)
21265371a3fSMartin Blumenstingl return 0;
21365371a3fSMartin Blumenstingl
21465371a3fSMartin Blumenstingl return le16_to_cpu(*(__le16 *)buf);
21565371a3fSMartin Blumenstingl }
21665371a3fSMartin Blumenstingl
21765371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
21865371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_READ);
21965371a3fSMartin Blumenstingl if (*err_ret)
22065371a3fSMartin Blumenstingl return 0;
22165371a3fSMartin Blumenstingl
22265371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
22365371a3fSMartin Blumenstingl return rtw_sdio_readw(rtwdev, reg_data, err_ret);
22465371a3fSMartin Blumenstingl }
22565371a3fSMartin Blumenstingl
rtw_sdio_indirect_read32(struct rtw_dev * rtwdev,u32 addr,int * err_ret)22665371a3fSMartin Blumenstingl static u32 rtw_sdio_indirect_read32(struct rtw_dev *rtwdev, u32 addr,
22765371a3fSMartin Blumenstingl int *err_ret)
22865371a3fSMartin Blumenstingl {
22965371a3fSMartin Blumenstingl u32 reg_data;
23065371a3fSMartin Blumenstingl u8 buf[4];
23165371a3fSMartin Blumenstingl
23265371a3fSMartin Blumenstingl if (!IS_ALIGNED(addr, 4)) {
23365371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_read_bytes(rtwdev, addr, buf, 4);
23465371a3fSMartin Blumenstingl if (*err_ret)
23565371a3fSMartin Blumenstingl return 0;
23665371a3fSMartin Blumenstingl
23765371a3fSMartin Blumenstingl return le32_to_cpu(*(__le32 *)buf);
23865371a3fSMartin Blumenstingl }
23965371a3fSMartin Blumenstingl
24065371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
24165371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_READ);
24265371a3fSMartin Blumenstingl if (*err_ret)
24365371a3fSMartin Blumenstingl return 0;
24465371a3fSMartin Blumenstingl
24565371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
24665371a3fSMartin Blumenstingl return rtw_sdio_readl(rtwdev, reg_data, err_ret);
24765371a3fSMartin Blumenstingl }
24865371a3fSMartin Blumenstingl
rtw_sdio_read8(struct rtw_dev * rtwdev,u32 addr)24965371a3fSMartin Blumenstingl static u8 rtw_sdio_read8(struct rtw_dev *rtwdev, u32 addr)
25065371a3fSMartin Blumenstingl {
25165371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
25265371a3fSMartin Blumenstingl bool direct, bus_claim;
25365371a3fSMartin Blumenstingl int ret;
25465371a3fSMartin Blumenstingl u8 val;
25565371a3fSMartin Blumenstingl
25665371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
25765371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
25865371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
25965371a3fSMartin Blumenstingl
26065371a3fSMartin Blumenstingl if (bus_claim)
26165371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
26265371a3fSMartin Blumenstingl
26365371a3fSMartin Blumenstingl if (direct)
26465371a3fSMartin Blumenstingl val = sdio_readb(rtwsdio->sdio_func, addr, &ret);
26565371a3fSMartin Blumenstingl else
26665371a3fSMartin Blumenstingl val = rtw_sdio_indirect_read8(rtwdev, addr, &ret);
26765371a3fSMartin Blumenstingl
26865371a3fSMartin Blumenstingl if (bus_claim)
26965371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
27065371a3fSMartin Blumenstingl
27165371a3fSMartin Blumenstingl if (ret)
27265371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio read8 failed (0x%x): %d", addr, ret);
27365371a3fSMartin Blumenstingl
27465371a3fSMartin Blumenstingl return val;
27565371a3fSMartin Blumenstingl }
27665371a3fSMartin Blumenstingl
rtw_sdio_read16(struct rtw_dev * rtwdev,u32 addr)27765371a3fSMartin Blumenstingl static u16 rtw_sdio_read16(struct rtw_dev *rtwdev, u32 addr)
27865371a3fSMartin Blumenstingl {
27965371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
28065371a3fSMartin Blumenstingl bool direct, bus_claim;
28165371a3fSMartin Blumenstingl int ret;
28265371a3fSMartin Blumenstingl u16 val;
28365371a3fSMartin Blumenstingl
28465371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
28565371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
28665371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
28765371a3fSMartin Blumenstingl
28865371a3fSMartin Blumenstingl if (bus_claim)
28965371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
29065371a3fSMartin Blumenstingl
29165371a3fSMartin Blumenstingl if (direct)
29265371a3fSMartin Blumenstingl val = rtw_sdio_readw(rtwdev, addr, &ret);
29365371a3fSMartin Blumenstingl else
29465371a3fSMartin Blumenstingl val = rtw_sdio_indirect_read16(rtwdev, addr, &ret);
29565371a3fSMartin Blumenstingl
29665371a3fSMartin Blumenstingl if (bus_claim)
29765371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
29865371a3fSMartin Blumenstingl
29965371a3fSMartin Blumenstingl if (ret)
30065371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio read16 failed (0x%x): %d", addr, ret);
30165371a3fSMartin Blumenstingl
30265371a3fSMartin Blumenstingl return val;
30365371a3fSMartin Blumenstingl }
30465371a3fSMartin Blumenstingl
rtw_sdio_read32(struct rtw_dev * rtwdev,u32 addr)30565371a3fSMartin Blumenstingl static u32 rtw_sdio_read32(struct rtw_dev *rtwdev, u32 addr)
30665371a3fSMartin Blumenstingl {
30765371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
30865371a3fSMartin Blumenstingl bool direct, bus_claim;
30965371a3fSMartin Blumenstingl u32 val;
31065371a3fSMartin Blumenstingl int ret;
31165371a3fSMartin Blumenstingl
31265371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
31365371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
31465371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
31565371a3fSMartin Blumenstingl
31665371a3fSMartin Blumenstingl if (bus_claim)
31765371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
31865371a3fSMartin Blumenstingl
31965371a3fSMartin Blumenstingl if (direct)
32065371a3fSMartin Blumenstingl val = rtw_sdio_readl(rtwdev, addr, &ret);
32165371a3fSMartin Blumenstingl else
32265371a3fSMartin Blumenstingl val = rtw_sdio_indirect_read32(rtwdev, addr, &ret);
32365371a3fSMartin Blumenstingl
32465371a3fSMartin Blumenstingl if (bus_claim)
32565371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
32665371a3fSMartin Blumenstingl
32765371a3fSMartin Blumenstingl if (ret)
32865371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio read32 failed (0x%x): %d", addr, ret);
32965371a3fSMartin Blumenstingl
33065371a3fSMartin Blumenstingl return val;
33165371a3fSMartin Blumenstingl }
33265371a3fSMartin Blumenstingl
rtw_sdio_indirect_write8(struct rtw_dev * rtwdev,u8 val,u32 addr,int * err_ret)33365371a3fSMartin Blumenstingl static void rtw_sdio_indirect_write8(struct rtw_dev *rtwdev, u8 val, u32 addr,
33465371a3fSMartin Blumenstingl int *err_ret)
33565371a3fSMartin Blumenstingl {
33665371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
33765371a3fSMartin Blumenstingl u32 reg_data;
33865371a3fSMartin Blumenstingl
33965371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
34065371a3fSMartin Blumenstingl sdio_writeb(rtwsdio->sdio_func, val, reg_data, err_ret);
34165371a3fSMartin Blumenstingl if (*err_ret)
34265371a3fSMartin Blumenstingl return;
34365371a3fSMartin Blumenstingl
34465371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
34565371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_WRITE);
34665371a3fSMartin Blumenstingl }
34765371a3fSMartin Blumenstingl
rtw_sdio_indirect_write16(struct rtw_dev * rtwdev,u16 val,u32 addr,int * err_ret)34865371a3fSMartin Blumenstingl static void rtw_sdio_indirect_write16(struct rtw_dev *rtwdev, u16 val, u32 addr,
34965371a3fSMartin Blumenstingl int *err_ret)
35065371a3fSMartin Blumenstingl {
35165371a3fSMartin Blumenstingl u32 reg_data;
35265371a3fSMartin Blumenstingl
35365371a3fSMartin Blumenstingl if (!IS_ALIGNED(addr, 2)) {
35465371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, true);
35565371a3fSMartin Blumenstingl rtw_sdio_writew(rtwdev, val, addr, err_ret);
35665371a3fSMartin Blumenstingl return;
35765371a3fSMartin Blumenstingl }
35865371a3fSMartin Blumenstingl
35965371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
36065371a3fSMartin Blumenstingl rtw_sdio_writew(rtwdev, val, reg_data, err_ret);
36165371a3fSMartin Blumenstingl if (*err_ret)
36265371a3fSMartin Blumenstingl return;
36365371a3fSMartin Blumenstingl
36465371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
36565371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_WRITE |
36665371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_WORD);
36765371a3fSMartin Blumenstingl }
36865371a3fSMartin Blumenstingl
rtw_sdio_indirect_write32(struct rtw_dev * rtwdev,u32 val,u32 addr,int * err_ret)36965371a3fSMartin Blumenstingl static void rtw_sdio_indirect_write32(struct rtw_dev *rtwdev, u32 val,
37065371a3fSMartin Blumenstingl u32 addr, int *err_ret)
37165371a3fSMartin Blumenstingl {
37265371a3fSMartin Blumenstingl u32 reg_data;
37365371a3fSMartin Blumenstingl
37465371a3fSMartin Blumenstingl if (!IS_ALIGNED(addr, 4)) {
37565371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, true);
37665371a3fSMartin Blumenstingl rtw_sdio_writel(rtwdev, val, addr, err_ret);
37765371a3fSMartin Blumenstingl return;
37865371a3fSMartin Blumenstingl }
37965371a3fSMartin Blumenstingl
38065371a3fSMartin Blumenstingl reg_data = rtw_sdio_to_bus_offset(rtwdev, REG_SDIO_INDIRECT_REG_DATA);
38165371a3fSMartin Blumenstingl rtw_sdio_writel(rtwdev, val, reg_data, err_ret);
38265371a3fSMartin Blumenstingl
38365371a3fSMartin Blumenstingl *err_ret = rtw_sdio_indirect_reg_cfg(rtwdev, addr,
38465371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_WRITE |
38565371a3fSMartin Blumenstingl BIT_SDIO_INDIRECT_REG_CFG_DWORD);
38665371a3fSMartin Blumenstingl }
38765371a3fSMartin Blumenstingl
rtw_sdio_write8(struct rtw_dev * rtwdev,u32 addr,u8 val)38865371a3fSMartin Blumenstingl static void rtw_sdio_write8(struct rtw_dev *rtwdev, u32 addr, u8 val)
38965371a3fSMartin Blumenstingl {
39065371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
39165371a3fSMartin Blumenstingl bool direct, bus_claim;
39265371a3fSMartin Blumenstingl int ret;
39365371a3fSMartin Blumenstingl
39465371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
39565371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
39665371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
39765371a3fSMartin Blumenstingl
39865371a3fSMartin Blumenstingl if (bus_claim)
39965371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
40065371a3fSMartin Blumenstingl
40165371a3fSMartin Blumenstingl if (direct)
40265371a3fSMartin Blumenstingl sdio_writeb(rtwsdio->sdio_func, val, addr, &ret);
40365371a3fSMartin Blumenstingl else
40465371a3fSMartin Blumenstingl rtw_sdio_indirect_write8(rtwdev, val, addr, &ret);
40565371a3fSMartin Blumenstingl
40665371a3fSMartin Blumenstingl if (bus_claim)
40765371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
40865371a3fSMartin Blumenstingl
40965371a3fSMartin Blumenstingl if (ret)
41065371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio write8 failed (0x%x): %d", addr, ret);
41165371a3fSMartin Blumenstingl }
41265371a3fSMartin Blumenstingl
rtw_sdio_write16(struct rtw_dev * rtwdev,u32 addr,u16 val)41365371a3fSMartin Blumenstingl static void rtw_sdio_write16(struct rtw_dev *rtwdev, u32 addr, u16 val)
41465371a3fSMartin Blumenstingl {
41565371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
41665371a3fSMartin Blumenstingl bool direct, bus_claim;
41765371a3fSMartin Blumenstingl int ret;
41865371a3fSMartin Blumenstingl
41965371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
42065371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
42165371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
42265371a3fSMartin Blumenstingl
42365371a3fSMartin Blumenstingl if (bus_claim)
42465371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
42565371a3fSMartin Blumenstingl
42665371a3fSMartin Blumenstingl if (direct)
42765371a3fSMartin Blumenstingl rtw_sdio_writew(rtwdev, val, addr, &ret);
42865371a3fSMartin Blumenstingl else
42965371a3fSMartin Blumenstingl rtw_sdio_indirect_write16(rtwdev, val, addr, &ret);
43065371a3fSMartin Blumenstingl
43165371a3fSMartin Blumenstingl if (bus_claim)
43265371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
43365371a3fSMartin Blumenstingl
43465371a3fSMartin Blumenstingl if (ret)
43565371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio write16 failed (0x%x): %d", addr, ret);
43665371a3fSMartin Blumenstingl }
43765371a3fSMartin Blumenstingl
rtw_sdio_write32(struct rtw_dev * rtwdev,u32 addr,u32 val)43865371a3fSMartin Blumenstingl static void rtw_sdio_write32(struct rtw_dev *rtwdev, u32 addr, u32 val)
43965371a3fSMartin Blumenstingl {
44065371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
44165371a3fSMartin Blumenstingl bool direct, bus_claim;
44265371a3fSMartin Blumenstingl int ret;
44365371a3fSMartin Blumenstingl
44465371a3fSMartin Blumenstingl direct = rtw_sdio_use_direct_io(rtwdev, addr);
44565371a3fSMartin Blumenstingl addr = rtw_sdio_to_io_address(rtwdev, addr, direct);
44665371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
44765371a3fSMartin Blumenstingl
44865371a3fSMartin Blumenstingl if (bus_claim)
44965371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
45065371a3fSMartin Blumenstingl
45165371a3fSMartin Blumenstingl if (direct)
45265371a3fSMartin Blumenstingl rtw_sdio_writel(rtwdev, val, addr, &ret);
45365371a3fSMartin Blumenstingl else
45465371a3fSMartin Blumenstingl rtw_sdio_indirect_write32(rtwdev, val, addr, &ret);
45565371a3fSMartin Blumenstingl
45665371a3fSMartin Blumenstingl if (bus_claim)
45765371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
45865371a3fSMartin Blumenstingl
45965371a3fSMartin Blumenstingl if (ret)
46065371a3fSMartin Blumenstingl rtw_warn(rtwdev, "sdio write32 failed (0x%x): %d", addr, ret);
46165371a3fSMartin Blumenstingl }
46265371a3fSMartin Blumenstingl
rtw_sdio_get_tx_addr(struct rtw_dev * rtwdev,size_t size,enum rtw_tx_queue_type queue)46365371a3fSMartin Blumenstingl static u32 rtw_sdio_get_tx_addr(struct rtw_dev *rtwdev, size_t size,
46465371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue)
46565371a3fSMartin Blumenstingl {
46665371a3fSMartin Blumenstingl u32 txaddr;
46765371a3fSMartin Blumenstingl
46865371a3fSMartin Blumenstingl switch (queue) {
46965371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BCN:
47065371a3fSMartin Blumenstingl case RTW_TX_QUEUE_H2C:
47165371a3fSMartin Blumenstingl case RTW_TX_QUEUE_HI0:
47265371a3fSMartin Blumenstingl txaddr = FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
47365371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_TXFF_HIGH);
47465371a3fSMartin Blumenstingl break;
47565371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VI:
47665371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VO:
47765371a3fSMartin Blumenstingl txaddr = FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
47865371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_TXFF_NORMAL);
47965371a3fSMartin Blumenstingl break;
48065371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BE:
48165371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BK:
48265371a3fSMartin Blumenstingl txaddr = FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
48365371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_TXFF_LOW);
48465371a3fSMartin Blumenstingl break;
48565371a3fSMartin Blumenstingl case RTW_TX_QUEUE_MGMT:
48665371a3fSMartin Blumenstingl txaddr = FIELD_PREP(REG_SDIO_CMD_ADDR_MSK,
48765371a3fSMartin Blumenstingl REG_SDIO_CMD_ADDR_TXFF_EXTRA);
48865371a3fSMartin Blumenstingl break;
48965371a3fSMartin Blumenstingl default:
49065371a3fSMartin Blumenstingl rtw_warn(rtwdev, "Unsupported queue for TX addr: 0x%02x\n",
49165371a3fSMartin Blumenstingl queue);
49265371a3fSMartin Blumenstingl return 0;
49365371a3fSMartin Blumenstingl }
49465371a3fSMartin Blumenstingl
49565371a3fSMartin Blumenstingl txaddr += DIV_ROUND_UP(size, 4);
49665371a3fSMartin Blumenstingl
49765371a3fSMartin Blumenstingl return txaddr;
49865371a3fSMartin Blumenstingl };
49965371a3fSMartin Blumenstingl
rtw_sdio_read_port(struct rtw_dev * rtwdev,u8 * buf,size_t count)50065371a3fSMartin Blumenstingl static int rtw_sdio_read_port(struct rtw_dev *rtwdev, u8 *buf, size_t count)
50165371a3fSMartin Blumenstingl {
50265371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
50300384f56SMartin Blumenstingl struct mmc_host *host = rtwsdio->sdio_func->card->host;
50465371a3fSMartin Blumenstingl bool bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
50565371a3fSMartin Blumenstingl u32 rxaddr = rtwsdio->rx_addr++;
50600384f56SMartin Blumenstingl int ret = 0, err;
50700384f56SMartin Blumenstingl size_t bytes;
50865371a3fSMartin Blumenstingl
50965371a3fSMartin Blumenstingl if (bus_claim)
51065371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
51165371a3fSMartin Blumenstingl
51200384f56SMartin Blumenstingl while (count > 0) {
51300384f56SMartin Blumenstingl bytes = min_t(size_t, host->max_req_size, count);
51400384f56SMartin Blumenstingl
51500384f56SMartin Blumenstingl err = sdio_memcpy_fromio(rtwsdio->sdio_func, buf,
51600384f56SMartin Blumenstingl RTW_SDIO_ADDR_RX_RX0FF_GEN(rxaddr),
51700384f56SMartin Blumenstingl bytes);
51800384f56SMartin Blumenstingl if (err) {
51965371a3fSMartin Blumenstingl rtw_warn(rtwdev,
52000384f56SMartin Blumenstingl "Failed to read %zu byte(s) from SDIO port 0x%08x: %d",
52100384f56SMartin Blumenstingl bytes, rxaddr, err);
52200384f56SMartin Blumenstingl
52300384f56SMartin Blumenstingl /* Signal to the caller that reading did not work and
52400384f56SMartin Blumenstingl * that the data in the buffer is short/corrupted.
52500384f56SMartin Blumenstingl */
52600384f56SMartin Blumenstingl ret = err;
52700384f56SMartin Blumenstingl
52800384f56SMartin Blumenstingl /* Don't stop here - instead drain the remaining data
52900384f56SMartin Blumenstingl * from the card's buffer, else the card will return
53000384f56SMartin Blumenstingl * corrupt data for the next rtw_sdio_read_port() call.
53100384f56SMartin Blumenstingl */
53200384f56SMartin Blumenstingl }
53300384f56SMartin Blumenstingl
53400384f56SMartin Blumenstingl count -= bytes;
53500384f56SMartin Blumenstingl buf += bytes;
53600384f56SMartin Blumenstingl }
53765371a3fSMartin Blumenstingl
53865371a3fSMartin Blumenstingl if (bus_claim)
53965371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
54065371a3fSMartin Blumenstingl
54165371a3fSMartin Blumenstingl return ret;
54265371a3fSMartin Blumenstingl }
54365371a3fSMartin Blumenstingl
rtw_sdio_check_free_txpg(struct rtw_dev * rtwdev,u8 queue,size_t count)54465371a3fSMartin Blumenstingl static int rtw_sdio_check_free_txpg(struct rtw_dev *rtwdev, u8 queue,
54565371a3fSMartin Blumenstingl size_t count)
54665371a3fSMartin Blumenstingl {
54765371a3fSMartin Blumenstingl unsigned int pages_free, pages_needed;
54865371a3fSMartin Blumenstingl
54965371a3fSMartin Blumenstingl if (rtw_chip_wcpu_11n(rtwdev)) {
55065371a3fSMartin Blumenstingl u32 free_txpg;
55165371a3fSMartin Blumenstingl
55265371a3fSMartin Blumenstingl free_txpg = rtw_sdio_read32(rtwdev, REG_SDIO_FREE_TXPG);
55365371a3fSMartin Blumenstingl
55465371a3fSMartin Blumenstingl switch (queue) {
55565371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BCN:
55665371a3fSMartin Blumenstingl case RTW_TX_QUEUE_H2C:
55765371a3fSMartin Blumenstingl case RTW_TX_QUEUE_HI0:
55865371a3fSMartin Blumenstingl case RTW_TX_QUEUE_MGMT:
55965371a3fSMartin Blumenstingl /* high */
56065371a3fSMartin Blumenstingl pages_free = free_txpg & 0xff;
56165371a3fSMartin Blumenstingl break;
56265371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VI:
56365371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VO:
56465371a3fSMartin Blumenstingl /* normal */
56565371a3fSMartin Blumenstingl pages_free = (free_txpg >> 8) & 0xff;
56665371a3fSMartin Blumenstingl break;
56765371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BE:
56865371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BK:
56965371a3fSMartin Blumenstingl /* low */
57065371a3fSMartin Blumenstingl pages_free = (free_txpg >> 16) & 0xff;
57165371a3fSMartin Blumenstingl break;
57265371a3fSMartin Blumenstingl default:
57365371a3fSMartin Blumenstingl rtw_warn(rtwdev, "Unknown mapping for queue %u\n", queue);
57465371a3fSMartin Blumenstingl return -EINVAL;
57565371a3fSMartin Blumenstingl }
57665371a3fSMartin Blumenstingl
57765371a3fSMartin Blumenstingl /* add the pages from the public queue */
57865371a3fSMartin Blumenstingl pages_free += (free_txpg >> 24) & 0xff;
57965371a3fSMartin Blumenstingl } else {
58065371a3fSMartin Blumenstingl u32 free_txpg[3];
58165371a3fSMartin Blumenstingl
58265371a3fSMartin Blumenstingl free_txpg[0] = rtw_sdio_read32(rtwdev, REG_SDIO_FREE_TXPG);
58365371a3fSMartin Blumenstingl free_txpg[1] = rtw_sdio_read32(rtwdev, REG_SDIO_FREE_TXPG + 4);
58465371a3fSMartin Blumenstingl free_txpg[2] = rtw_sdio_read32(rtwdev, REG_SDIO_FREE_TXPG + 8);
58565371a3fSMartin Blumenstingl
58665371a3fSMartin Blumenstingl switch (queue) {
58765371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BCN:
58865371a3fSMartin Blumenstingl case RTW_TX_QUEUE_H2C:
58965371a3fSMartin Blumenstingl case RTW_TX_QUEUE_HI0:
59065371a3fSMartin Blumenstingl /* high */
59165371a3fSMartin Blumenstingl pages_free = free_txpg[0] & 0xfff;
59265371a3fSMartin Blumenstingl break;
59365371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VI:
59465371a3fSMartin Blumenstingl case RTW_TX_QUEUE_VO:
59565371a3fSMartin Blumenstingl /* normal */
59665371a3fSMartin Blumenstingl pages_free = (free_txpg[0] >> 16) & 0xfff;
59765371a3fSMartin Blumenstingl break;
59865371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BE:
59965371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BK:
60065371a3fSMartin Blumenstingl /* low */
60165371a3fSMartin Blumenstingl pages_free = free_txpg[1] & 0xfff;
60265371a3fSMartin Blumenstingl break;
60365371a3fSMartin Blumenstingl case RTW_TX_QUEUE_MGMT:
60465371a3fSMartin Blumenstingl /* extra */
60565371a3fSMartin Blumenstingl pages_free = free_txpg[2] & 0xfff;
60665371a3fSMartin Blumenstingl break;
60765371a3fSMartin Blumenstingl default:
60865371a3fSMartin Blumenstingl rtw_warn(rtwdev, "Unknown mapping for queue %u\n", queue);
60965371a3fSMartin Blumenstingl return -EINVAL;
61065371a3fSMartin Blumenstingl }
61165371a3fSMartin Blumenstingl
61265371a3fSMartin Blumenstingl /* add the pages from the public queue */
61365371a3fSMartin Blumenstingl pages_free += (free_txpg[1] >> 16) & 0xfff;
61465371a3fSMartin Blumenstingl }
61565371a3fSMartin Blumenstingl
61665371a3fSMartin Blumenstingl pages_needed = DIV_ROUND_UP(count, rtwdev->chip->page_size);
61765371a3fSMartin Blumenstingl
61865371a3fSMartin Blumenstingl if (pages_needed > pages_free) {
61965371a3fSMartin Blumenstingl rtw_dbg(rtwdev, RTW_DBG_SDIO,
62065371a3fSMartin Blumenstingl "Not enough free pages (%u needed, %u free) in queue %u for %zu bytes\n",
62165371a3fSMartin Blumenstingl pages_needed, pages_free, queue, count);
62265371a3fSMartin Blumenstingl return -EBUSY;
62365371a3fSMartin Blumenstingl }
62465371a3fSMartin Blumenstingl
62565371a3fSMartin Blumenstingl return 0;
62665371a3fSMartin Blumenstingl }
62765371a3fSMartin Blumenstingl
rtw_sdio_write_port(struct rtw_dev * rtwdev,struct sk_buff * skb,enum rtw_tx_queue_type queue)62865371a3fSMartin Blumenstingl static int rtw_sdio_write_port(struct rtw_dev *rtwdev, struct sk_buff *skb,
62965371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue)
63065371a3fSMartin Blumenstingl {
63165371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
63265371a3fSMartin Blumenstingl bool bus_claim;
63365371a3fSMartin Blumenstingl size_t txsize;
63465371a3fSMartin Blumenstingl u32 txaddr;
63565371a3fSMartin Blumenstingl int ret;
63665371a3fSMartin Blumenstingl
63765371a3fSMartin Blumenstingl txaddr = rtw_sdio_get_tx_addr(rtwdev, skb->len, queue);
63865371a3fSMartin Blumenstingl if (!txaddr)
63965371a3fSMartin Blumenstingl return -EINVAL;
64065371a3fSMartin Blumenstingl
64165371a3fSMartin Blumenstingl txsize = sdio_align_size(rtwsdio->sdio_func, skb->len);
64265371a3fSMartin Blumenstingl
64365371a3fSMartin Blumenstingl ret = rtw_sdio_check_free_txpg(rtwdev, queue, txsize);
64465371a3fSMartin Blumenstingl if (ret)
64565371a3fSMartin Blumenstingl return ret;
64665371a3fSMartin Blumenstingl
64765371a3fSMartin Blumenstingl if (!IS_ALIGNED((unsigned long)skb->data, RTW_SDIO_DATA_PTR_ALIGN))
64865371a3fSMartin Blumenstingl rtw_warn(rtwdev, "Got unaligned SKB in %s() for queue %u\n",
64965371a3fSMartin Blumenstingl __func__, queue);
65065371a3fSMartin Blumenstingl
65165371a3fSMartin Blumenstingl bus_claim = rtw_sdio_bus_claim_needed(rtwsdio);
65265371a3fSMartin Blumenstingl
65365371a3fSMartin Blumenstingl if (bus_claim)
65465371a3fSMartin Blumenstingl sdio_claim_host(rtwsdio->sdio_func);
65565371a3fSMartin Blumenstingl
65665371a3fSMartin Blumenstingl ret = sdio_memcpy_toio(rtwsdio->sdio_func, txaddr, skb->data, txsize);
65765371a3fSMartin Blumenstingl
65865371a3fSMartin Blumenstingl if (bus_claim)
65965371a3fSMartin Blumenstingl sdio_release_host(rtwsdio->sdio_func);
66065371a3fSMartin Blumenstingl
66165371a3fSMartin Blumenstingl if (ret)
66265371a3fSMartin Blumenstingl rtw_warn(rtwdev,
66365371a3fSMartin Blumenstingl "Failed to write %zu byte(s) to SDIO port 0x%08x",
66465371a3fSMartin Blumenstingl txsize, txaddr);
66565371a3fSMartin Blumenstingl
66665371a3fSMartin Blumenstingl return ret;
66765371a3fSMartin Blumenstingl }
66865371a3fSMartin Blumenstingl
rtw_sdio_init(struct rtw_dev * rtwdev)66965371a3fSMartin Blumenstingl static void rtw_sdio_init(struct rtw_dev *rtwdev)
67065371a3fSMartin Blumenstingl {
67165371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
67265371a3fSMartin Blumenstingl
67365371a3fSMartin Blumenstingl rtwsdio->irq_mask = REG_SDIO_HIMR_RX_REQUEST | REG_SDIO_HIMR_CPWM1;
67465371a3fSMartin Blumenstingl }
67565371a3fSMartin Blumenstingl
rtw_sdio_enable_rx_aggregation(struct rtw_dev * rtwdev)67665371a3fSMartin Blumenstingl static void rtw_sdio_enable_rx_aggregation(struct rtw_dev *rtwdev)
67765371a3fSMartin Blumenstingl {
67865371a3fSMartin Blumenstingl u8 size, timeout;
67965371a3fSMartin Blumenstingl
68065371a3fSMartin Blumenstingl if (rtw_chip_wcpu_11n(rtwdev)) {
68165371a3fSMartin Blumenstingl size = 0x6;
68265371a3fSMartin Blumenstingl timeout = 0x6;
68365371a3fSMartin Blumenstingl } else {
68465371a3fSMartin Blumenstingl size = 0xff;
68565371a3fSMartin Blumenstingl timeout = 0x1;
68665371a3fSMartin Blumenstingl }
68765371a3fSMartin Blumenstingl
68865371a3fSMartin Blumenstingl /* Make the firmware honor the size limit configured below */
68965371a3fSMartin Blumenstingl rtw_write32_set(rtwdev, REG_RXDMA_AGG_PG_TH, BIT_EN_PRE_CALC);
69065371a3fSMartin Blumenstingl
69165371a3fSMartin Blumenstingl rtw_write8_set(rtwdev, REG_TXDMA_PQ_MAP, BIT_RXDMA_AGG_EN);
69265371a3fSMartin Blumenstingl
69365371a3fSMartin Blumenstingl rtw_write16(rtwdev, REG_RXDMA_AGG_PG_TH,
69465371a3fSMartin Blumenstingl FIELD_PREP(BIT_RXDMA_AGG_PG_TH, size) |
69565371a3fSMartin Blumenstingl FIELD_PREP(BIT_DMA_AGG_TO_V1, timeout));
69665371a3fSMartin Blumenstingl
69765371a3fSMartin Blumenstingl rtw_write8_set(rtwdev, REG_RXDMA_MODE, BIT_DMA_MODE);
69865371a3fSMartin Blumenstingl }
69965371a3fSMartin Blumenstingl
rtw_sdio_enable_interrupt(struct rtw_dev * rtwdev)70065371a3fSMartin Blumenstingl static void rtw_sdio_enable_interrupt(struct rtw_dev *rtwdev)
70165371a3fSMartin Blumenstingl {
70265371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
70365371a3fSMartin Blumenstingl
70465371a3fSMartin Blumenstingl rtw_write32(rtwdev, REG_SDIO_HIMR, rtwsdio->irq_mask);
70565371a3fSMartin Blumenstingl }
70665371a3fSMartin Blumenstingl
rtw_sdio_disable_interrupt(struct rtw_dev * rtwdev)70765371a3fSMartin Blumenstingl static void rtw_sdio_disable_interrupt(struct rtw_dev *rtwdev)
70865371a3fSMartin Blumenstingl {
70965371a3fSMartin Blumenstingl rtw_write32(rtwdev, REG_SDIO_HIMR, 0x0);
71065371a3fSMartin Blumenstingl }
71165371a3fSMartin Blumenstingl
rtw_sdio_get_tx_qsel(struct rtw_dev * rtwdev,struct sk_buff * skb,u8 queue)71265371a3fSMartin Blumenstingl static u8 rtw_sdio_get_tx_qsel(struct rtw_dev *rtwdev, struct sk_buff *skb,
71365371a3fSMartin Blumenstingl u8 queue)
71465371a3fSMartin Blumenstingl {
71565371a3fSMartin Blumenstingl switch (queue) {
71665371a3fSMartin Blumenstingl case RTW_TX_QUEUE_BCN:
71765371a3fSMartin Blumenstingl return TX_DESC_QSEL_BEACON;
71865371a3fSMartin Blumenstingl case RTW_TX_QUEUE_H2C:
71965371a3fSMartin Blumenstingl return TX_DESC_QSEL_H2C;
72065371a3fSMartin Blumenstingl case RTW_TX_QUEUE_MGMT:
72165371a3fSMartin Blumenstingl if (rtw_chip_wcpu_11n(rtwdev))
72265371a3fSMartin Blumenstingl return TX_DESC_QSEL_HIGH;
72365371a3fSMartin Blumenstingl else
72465371a3fSMartin Blumenstingl return TX_DESC_QSEL_MGMT;
72565371a3fSMartin Blumenstingl case RTW_TX_QUEUE_HI0:
72665371a3fSMartin Blumenstingl return TX_DESC_QSEL_HIGH;
72765371a3fSMartin Blumenstingl default:
72865371a3fSMartin Blumenstingl return skb->priority;
72965371a3fSMartin Blumenstingl }
73065371a3fSMartin Blumenstingl }
73165371a3fSMartin Blumenstingl
rtw_sdio_setup(struct rtw_dev * rtwdev)73265371a3fSMartin Blumenstingl static int rtw_sdio_setup(struct rtw_dev *rtwdev)
73365371a3fSMartin Blumenstingl {
73465371a3fSMartin Blumenstingl /* nothing to do */
73565371a3fSMartin Blumenstingl return 0;
73665371a3fSMartin Blumenstingl }
73765371a3fSMartin Blumenstingl
rtw_sdio_start(struct rtw_dev * rtwdev)73865371a3fSMartin Blumenstingl static int rtw_sdio_start(struct rtw_dev *rtwdev)
73965371a3fSMartin Blumenstingl {
74065371a3fSMartin Blumenstingl rtw_sdio_enable_rx_aggregation(rtwdev);
74165371a3fSMartin Blumenstingl rtw_sdio_enable_interrupt(rtwdev);
74265371a3fSMartin Blumenstingl
74365371a3fSMartin Blumenstingl return 0;
74465371a3fSMartin Blumenstingl }
74565371a3fSMartin Blumenstingl
rtw_sdio_stop(struct rtw_dev * rtwdev)74665371a3fSMartin Blumenstingl static void rtw_sdio_stop(struct rtw_dev *rtwdev)
74765371a3fSMartin Blumenstingl {
74865371a3fSMartin Blumenstingl rtw_sdio_disable_interrupt(rtwdev);
74965371a3fSMartin Blumenstingl }
75065371a3fSMartin Blumenstingl
rtw_sdio_deep_ps_enter(struct rtw_dev * rtwdev)75165371a3fSMartin Blumenstingl static void rtw_sdio_deep_ps_enter(struct rtw_dev *rtwdev)
75265371a3fSMartin Blumenstingl {
75365371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
75465371a3fSMartin Blumenstingl bool tx_empty = true;
75565371a3fSMartin Blumenstingl u8 queue;
75665371a3fSMartin Blumenstingl
75765371a3fSMartin Blumenstingl if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_TX_WAKE)) {
75865371a3fSMartin Blumenstingl /* Deep PS state is not allowed to TX-DMA */
75965371a3fSMartin Blumenstingl for (queue = 0; queue < RTK_MAX_TX_QUEUE_NUM; queue++) {
76065371a3fSMartin Blumenstingl /* BCN queue is rsvd page, does not have DMA interrupt
76165371a3fSMartin Blumenstingl * H2C queue is managed by firmware
76265371a3fSMartin Blumenstingl */
76365371a3fSMartin Blumenstingl if (queue == RTW_TX_QUEUE_BCN ||
76465371a3fSMartin Blumenstingl queue == RTW_TX_QUEUE_H2C)
76565371a3fSMartin Blumenstingl continue;
76665371a3fSMartin Blumenstingl
76765371a3fSMartin Blumenstingl /* check if there is any skb DMAing */
76865371a3fSMartin Blumenstingl if (skb_queue_len(&rtwsdio->tx_queue[queue])) {
76965371a3fSMartin Blumenstingl tx_empty = false;
77065371a3fSMartin Blumenstingl break;
77165371a3fSMartin Blumenstingl }
77265371a3fSMartin Blumenstingl }
77365371a3fSMartin Blumenstingl }
77465371a3fSMartin Blumenstingl
77565371a3fSMartin Blumenstingl if (!tx_empty) {
77665371a3fSMartin Blumenstingl rtw_dbg(rtwdev, RTW_DBG_PS,
77765371a3fSMartin Blumenstingl "TX path not empty, cannot enter deep power save state\n");
77865371a3fSMartin Blumenstingl return;
77965371a3fSMartin Blumenstingl }
78065371a3fSMartin Blumenstingl
78165371a3fSMartin Blumenstingl set_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags);
78265371a3fSMartin Blumenstingl rtw_power_mode_change(rtwdev, true);
78365371a3fSMartin Blumenstingl }
78465371a3fSMartin Blumenstingl
rtw_sdio_deep_ps_leave(struct rtw_dev * rtwdev)78565371a3fSMartin Blumenstingl static void rtw_sdio_deep_ps_leave(struct rtw_dev *rtwdev)
78665371a3fSMartin Blumenstingl {
78765371a3fSMartin Blumenstingl if (test_and_clear_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags))
78865371a3fSMartin Blumenstingl rtw_power_mode_change(rtwdev, false);
78965371a3fSMartin Blumenstingl }
79065371a3fSMartin Blumenstingl
rtw_sdio_deep_ps(struct rtw_dev * rtwdev,bool enter)79165371a3fSMartin Blumenstingl static void rtw_sdio_deep_ps(struct rtw_dev *rtwdev, bool enter)
79265371a3fSMartin Blumenstingl {
79365371a3fSMartin Blumenstingl if (enter && !test_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags))
79465371a3fSMartin Blumenstingl rtw_sdio_deep_ps_enter(rtwdev);
79565371a3fSMartin Blumenstingl
79665371a3fSMartin Blumenstingl if (!enter && test_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags))
79765371a3fSMartin Blumenstingl rtw_sdio_deep_ps_leave(rtwdev);
79865371a3fSMartin Blumenstingl }
79965371a3fSMartin Blumenstingl
rtw_sdio_tx_kick_off(struct rtw_dev * rtwdev)80065371a3fSMartin Blumenstingl static void rtw_sdio_tx_kick_off(struct rtw_dev *rtwdev)
80165371a3fSMartin Blumenstingl {
80265371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
80365371a3fSMartin Blumenstingl
80465371a3fSMartin Blumenstingl queue_work(rtwsdio->txwq, &rtwsdio->tx_handler_data->work);
80565371a3fSMartin Blumenstingl }
80665371a3fSMartin Blumenstingl
rtw_sdio_link_ps(struct rtw_dev * rtwdev,bool enter)80765371a3fSMartin Blumenstingl static void rtw_sdio_link_ps(struct rtw_dev *rtwdev, bool enter)
80865371a3fSMartin Blumenstingl {
80965371a3fSMartin Blumenstingl /* nothing to do */
81065371a3fSMartin Blumenstingl }
81165371a3fSMartin Blumenstingl
rtw_sdio_interface_cfg(struct rtw_dev * rtwdev)81265371a3fSMartin Blumenstingl static void rtw_sdio_interface_cfg(struct rtw_dev *rtwdev)
81365371a3fSMartin Blumenstingl {
81465371a3fSMartin Blumenstingl u32 val;
81565371a3fSMartin Blumenstingl
81665371a3fSMartin Blumenstingl rtw_read32(rtwdev, REG_SDIO_FREE_TXPG);
81765371a3fSMartin Blumenstingl
81865371a3fSMartin Blumenstingl val = rtw_read32(rtwdev, REG_SDIO_TX_CTRL);
81965371a3fSMartin Blumenstingl val &= 0xfff8;
82065371a3fSMartin Blumenstingl rtw_write32(rtwdev, REG_SDIO_TX_CTRL, val);
82165371a3fSMartin Blumenstingl }
82265371a3fSMartin Blumenstingl
rtw_sdio_get_tx_data(struct sk_buff * skb)82365371a3fSMartin Blumenstingl static struct rtw_sdio_tx_data *rtw_sdio_get_tx_data(struct sk_buff *skb)
82465371a3fSMartin Blumenstingl {
82565371a3fSMartin Blumenstingl struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
82665371a3fSMartin Blumenstingl
82765371a3fSMartin Blumenstingl BUILD_BUG_ON(sizeof(struct rtw_sdio_tx_data) >
82865371a3fSMartin Blumenstingl sizeof(info->status.status_driver_data));
82965371a3fSMartin Blumenstingl
83065371a3fSMartin Blumenstingl return (struct rtw_sdio_tx_data *)info->status.status_driver_data;
83165371a3fSMartin Blumenstingl }
83265371a3fSMartin Blumenstingl
rtw_sdio_tx_skb_prepare(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,enum rtw_tx_queue_type queue)83365371a3fSMartin Blumenstingl static void rtw_sdio_tx_skb_prepare(struct rtw_dev *rtwdev,
83465371a3fSMartin Blumenstingl struct rtw_tx_pkt_info *pkt_info,
83565371a3fSMartin Blumenstingl struct sk_buff *skb,
83665371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue)
83765371a3fSMartin Blumenstingl {
83865371a3fSMartin Blumenstingl const struct rtw_chip_info *chip = rtwdev->chip;
83965371a3fSMartin Blumenstingl unsigned long data_addr, aligned_addr;
84065371a3fSMartin Blumenstingl size_t offset;
84165371a3fSMartin Blumenstingl u8 *pkt_desc;
84265371a3fSMartin Blumenstingl
84365371a3fSMartin Blumenstingl pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
84465371a3fSMartin Blumenstingl
84565371a3fSMartin Blumenstingl data_addr = (unsigned long)pkt_desc;
84665371a3fSMartin Blumenstingl aligned_addr = ALIGN(data_addr, RTW_SDIO_DATA_PTR_ALIGN);
84765371a3fSMartin Blumenstingl
84865371a3fSMartin Blumenstingl if (data_addr != aligned_addr) {
84965371a3fSMartin Blumenstingl /* Ensure that the start of the pkt_desc is always aligned at
85065371a3fSMartin Blumenstingl * RTW_SDIO_DATA_PTR_ALIGN.
85165371a3fSMartin Blumenstingl */
85265371a3fSMartin Blumenstingl offset = RTW_SDIO_DATA_PTR_ALIGN - (aligned_addr - data_addr);
85365371a3fSMartin Blumenstingl
85465371a3fSMartin Blumenstingl pkt_desc = skb_push(skb, offset);
85565371a3fSMartin Blumenstingl
85665371a3fSMartin Blumenstingl /* By inserting padding to align the start of the pkt_desc we
85765371a3fSMartin Blumenstingl * need to inform the firmware that the actual data starts at
85865371a3fSMartin Blumenstingl * a different offset than normal.
85965371a3fSMartin Blumenstingl */
86065371a3fSMartin Blumenstingl pkt_info->offset += offset;
86165371a3fSMartin Blumenstingl }
86265371a3fSMartin Blumenstingl
86365371a3fSMartin Blumenstingl memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
86465371a3fSMartin Blumenstingl
86565371a3fSMartin Blumenstingl pkt_info->qsel = rtw_sdio_get_tx_qsel(rtwdev, skb, queue);
86665371a3fSMartin Blumenstingl
86765371a3fSMartin Blumenstingl rtw_tx_fill_tx_desc(pkt_info, skb);
86865371a3fSMartin Blumenstingl rtw_tx_fill_txdesc_checksum(rtwdev, pkt_info, pkt_desc);
86965371a3fSMartin Blumenstingl }
87065371a3fSMartin Blumenstingl
rtw_sdio_write_data(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb,enum rtw_tx_queue_type queue)87165371a3fSMartin Blumenstingl static int rtw_sdio_write_data(struct rtw_dev *rtwdev,
87265371a3fSMartin Blumenstingl struct rtw_tx_pkt_info *pkt_info,
87365371a3fSMartin Blumenstingl struct sk_buff *skb,
87465371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue)
87565371a3fSMartin Blumenstingl {
87665371a3fSMartin Blumenstingl int ret;
87765371a3fSMartin Blumenstingl
87865371a3fSMartin Blumenstingl rtw_sdio_tx_skb_prepare(rtwdev, pkt_info, skb, queue);
87965371a3fSMartin Blumenstingl
88065371a3fSMartin Blumenstingl ret = rtw_sdio_write_port(rtwdev, skb, queue);
88165371a3fSMartin Blumenstingl dev_kfree_skb_any(skb);
88265371a3fSMartin Blumenstingl
88365371a3fSMartin Blumenstingl return ret;
88465371a3fSMartin Blumenstingl }
88565371a3fSMartin Blumenstingl
rtw_sdio_write_data_rsvd_page(struct rtw_dev * rtwdev,u8 * buf,u32 size)88665371a3fSMartin Blumenstingl static int rtw_sdio_write_data_rsvd_page(struct rtw_dev *rtwdev, u8 *buf,
88765371a3fSMartin Blumenstingl u32 size)
88865371a3fSMartin Blumenstingl {
88965371a3fSMartin Blumenstingl struct rtw_tx_pkt_info pkt_info = {};
89065371a3fSMartin Blumenstingl struct sk_buff *skb;
89165371a3fSMartin Blumenstingl
89265371a3fSMartin Blumenstingl skb = rtw_tx_write_data_rsvd_page_get(rtwdev, &pkt_info, buf, size);
89365371a3fSMartin Blumenstingl if (!skb)
89465371a3fSMartin Blumenstingl return -ENOMEM;
89565371a3fSMartin Blumenstingl
89665371a3fSMartin Blumenstingl return rtw_sdio_write_data(rtwdev, &pkt_info, skb, RTW_TX_QUEUE_BCN);
89765371a3fSMartin Blumenstingl }
89865371a3fSMartin Blumenstingl
rtw_sdio_write_data_h2c(struct rtw_dev * rtwdev,u8 * buf,u32 size)89965371a3fSMartin Blumenstingl static int rtw_sdio_write_data_h2c(struct rtw_dev *rtwdev, u8 *buf, u32 size)
90065371a3fSMartin Blumenstingl {
90165371a3fSMartin Blumenstingl struct rtw_tx_pkt_info pkt_info = {};
90265371a3fSMartin Blumenstingl struct sk_buff *skb;
90365371a3fSMartin Blumenstingl
90465371a3fSMartin Blumenstingl skb = rtw_tx_write_data_h2c_get(rtwdev, &pkt_info, buf, size);
90565371a3fSMartin Blumenstingl if (!skb)
90665371a3fSMartin Blumenstingl return -ENOMEM;
90765371a3fSMartin Blumenstingl
90865371a3fSMartin Blumenstingl return rtw_sdio_write_data(rtwdev, &pkt_info, skb, RTW_TX_QUEUE_H2C);
90965371a3fSMartin Blumenstingl }
91065371a3fSMartin Blumenstingl
rtw_sdio_tx_write(struct rtw_dev * rtwdev,struct rtw_tx_pkt_info * pkt_info,struct sk_buff * skb)91165371a3fSMartin Blumenstingl static int rtw_sdio_tx_write(struct rtw_dev *rtwdev,
91265371a3fSMartin Blumenstingl struct rtw_tx_pkt_info *pkt_info,
91365371a3fSMartin Blumenstingl struct sk_buff *skb)
91465371a3fSMartin Blumenstingl {
91565371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
91665371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue = rtw_tx_queue_mapping(skb);
91765371a3fSMartin Blumenstingl struct rtw_sdio_tx_data *tx_data;
91865371a3fSMartin Blumenstingl
91965371a3fSMartin Blumenstingl rtw_sdio_tx_skb_prepare(rtwdev, pkt_info, skb, queue);
92065371a3fSMartin Blumenstingl
92165371a3fSMartin Blumenstingl tx_data = rtw_sdio_get_tx_data(skb);
92265371a3fSMartin Blumenstingl tx_data->sn = pkt_info->sn;
92365371a3fSMartin Blumenstingl
92465371a3fSMartin Blumenstingl skb_queue_tail(&rtwsdio->tx_queue[queue], skb);
92565371a3fSMartin Blumenstingl
92665371a3fSMartin Blumenstingl return 0;
92765371a3fSMartin Blumenstingl }
92865371a3fSMartin Blumenstingl
rtw_sdio_tx_err_isr(struct rtw_dev * rtwdev)92965371a3fSMartin Blumenstingl static void rtw_sdio_tx_err_isr(struct rtw_dev *rtwdev)
93065371a3fSMartin Blumenstingl {
93165371a3fSMartin Blumenstingl u32 val = rtw_read32(rtwdev, REG_TXDMA_STATUS);
93265371a3fSMartin Blumenstingl
93365371a3fSMartin Blumenstingl rtw_write32(rtwdev, REG_TXDMA_STATUS, val);
93465371a3fSMartin Blumenstingl }
93565371a3fSMartin Blumenstingl
rtw_sdio_rx_skb(struct rtw_dev * rtwdev,struct sk_buff * skb,u32 pkt_offset,struct rtw_rx_pkt_stat * pkt_stat,struct ieee80211_rx_status * rx_status)93665371a3fSMartin Blumenstingl static void rtw_sdio_rx_skb(struct rtw_dev *rtwdev, struct sk_buff *skb,
93765371a3fSMartin Blumenstingl u32 pkt_offset, struct rtw_rx_pkt_stat *pkt_stat,
93865371a3fSMartin Blumenstingl struct ieee80211_rx_status *rx_status)
93965371a3fSMartin Blumenstingl {
94065371a3fSMartin Blumenstingl *IEEE80211_SKB_RXCB(skb) = *rx_status;
94165371a3fSMartin Blumenstingl
94265371a3fSMartin Blumenstingl if (pkt_stat->is_c2h) {
94365371a3fSMartin Blumenstingl skb_put(skb, pkt_stat->pkt_len + pkt_offset);
94465371a3fSMartin Blumenstingl rtw_fw_c2h_cmd_rx_irqsafe(rtwdev, pkt_offset, skb);
94565371a3fSMartin Blumenstingl return;
94665371a3fSMartin Blumenstingl }
94765371a3fSMartin Blumenstingl
94865371a3fSMartin Blumenstingl skb_put(skb, pkt_stat->pkt_len);
94965371a3fSMartin Blumenstingl skb_reserve(skb, pkt_offset);
95065371a3fSMartin Blumenstingl
95153ed4b25SPo-Hao Huang rtw_update_rx_freq_for_invalid(rtwdev, skb, rx_status, pkt_stat);
95265371a3fSMartin Blumenstingl rtw_rx_stats(rtwdev, pkt_stat->vif, skb);
95365371a3fSMartin Blumenstingl
95465371a3fSMartin Blumenstingl ieee80211_rx_irqsafe(rtwdev->hw, skb);
95565371a3fSMartin Blumenstingl }
95665371a3fSMartin Blumenstingl
rtw_sdio_rxfifo_recv(struct rtw_dev * rtwdev,u32 rx_len)95765371a3fSMartin Blumenstingl static void rtw_sdio_rxfifo_recv(struct rtw_dev *rtwdev, u32 rx_len)
95865371a3fSMartin Blumenstingl {
95965371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
96065371a3fSMartin Blumenstingl const struct rtw_chip_info *chip = rtwdev->chip;
96165371a3fSMartin Blumenstingl u32 pkt_desc_sz = chip->rx_pkt_desc_sz;
96265371a3fSMartin Blumenstingl struct ieee80211_rx_status rx_status;
96365371a3fSMartin Blumenstingl struct rtw_rx_pkt_stat pkt_stat;
96465371a3fSMartin Blumenstingl struct sk_buff *skb, *split_skb;
96565371a3fSMartin Blumenstingl u32 pkt_offset, curr_pkt_len;
96665371a3fSMartin Blumenstingl size_t bufsz;
96765371a3fSMartin Blumenstingl u8 *rx_desc;
96865371a3fSMartin Blumenstingl int ret;
96965371a3fSMartin Blumenstingl
97065371a3fSMartin Blumenstingl bufsz = sdio_align_size(rtwsdio->sdio_func, rx_len);
97165371a3fSMartin Blumenstingl
97265371a3fSMartin Blumenstingl skb = dev_alloc_skb(bufsz);
97365371a3fSMartin Blumenstingl if (!skb)
97465371a3fSMartin Blumenstingl return;
97565371a3fSMartin Blumenstingl
97665371a3fSMartin Blumenstingl ret = rtw_sdio_read_port(rtwdev, skb->data, bufsz);
97765371a3fSMartin Blumenstingl if (ret) {
97865371a3fSMartin Blumenstingl dev_kfree_skb_any(skb);
97965371a3fSMartin Blumenstingl return;
98065371a3fSMartin Blumenstingl }
98165371a3fSMartin Blumenstingl
98265371a3fSMartin Blumenstingl while (true) {
98365371a3fSMartin Blumenstingl rx_desc = skb->data;
98465371a3fSMartin Blumenstingl chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat,
98565371a3fSMartin Blumenstingl &rx_status);
98665371a3fSMartin Blumenstingl pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz +
98765371a3fSMartin Blumenstingl pkt_stat.shift;
98865371a3fSMartin Blumenstingl
98965371a3fSMartin Blumenstingl curr_pkt_len = ALIGN(pkt_offset + pkt_stat.pkt_len,
99065371a3fSMartin Blumenstingl RTW_SDIO_DATA_PTR_ALIGN);
99165371a3fSMartin Blumenstingl
99265371a3fSMartin Blumenstingl if ((curr_pkt_len + pkt_desc_sz) >= rx_len) {
99365371a3fSMartin Blumenstingl /* Use the original skb (with it's adjusted offset)
99465371a3fSMartin Blumenstingl * when processing the last (or even the only) entry to
99565371a3fSMartin Blumenstingl * have it's memory freed automatically.
99665371a3fSMartin Blumenstingl */
99765371a3fSMartin Blumenstingl rtw_sdio_rx_skb(rtwdev, skb, pkt_offset, &pkt_stat,
99865371a3fSMartin Blumenstingl &rx_status);
99965371a3fSMartin Blumenstingl break;
100065371a3fSMartin Blumenstingl }
100165371a3fSMartin Blumenstingl
100265371a3fSMartin Blumenstingl split_skb = dev_alloc_skb(curr_pkt_len);
100365371a3fSMartin Blumenstingl if (!split_skb) {
100465371a3fSMartin Blumenstingl rtw_sdio_rx_skb(rtwdev, skb, pkt_offset, &pkt_stat,
100565371a3fSMartin Blumenstingl &rx_status);
100665371a3fSMartin Blumenstingl break;
100765371a3fSMartin Blumenstingl }
100865371a3fSMartin Blumenstingl
100965371a3fSMartin Blumenstingl skb_copy_header(split_skb, skb);
101065371a3fSMartin Blumenstingl memcpy(split_skb->data, skb->data, curr_pkt_len);
101165371a3fSMartin Blumenstingl
101265371a3fSMartin Blumenstingl rtw_sdio_rx_skb(rtwdev, split_skb, pkt_offset, &pkt_stat,
101365371a3fSMartin Blumenstingl &rx_status);
101465371a3fSMartin Blumenstingl
101565371a3fSMartin Blumenstingl /* Move to the start of the next RX descriptor */
101665371a3fSMartin Blumenstingl skb_reserve(skb, curr_pkt_len);
101765371a3fSMartin Blumenstingl rx_len -= curr_pkt_len;
101865371a3fSMartin Blumenstingl }
101965371a3fSMartin Blumenstingl }
102065371a3fSMartin Blumenstingl
rtw_sdio_rx_isr(struct rtw_dev * rtwdev)102165371a3fSMartin Blumenstingl static void rtw_sdio_rx_isr(struct rtw_dev *rtwdev)
102265371a3fSMartin Blumenstingl {
1023e967229eSMartin Blumenstingl u32 rx_len, hisr, total_rx_bytes = 0;
102465371a3fSMartin Blumenstingl
1025e967229eSMartin Blumenstingl do {
102665371a3fSMartin Blumenstingl if (rtw_chip_wcpu_11n(rtwdev))
102765371a3fSMartin Blumenstingl rx_len = rtw_read16(rtwdev, REG_SDIO_RX0_REQ_LEN);
102865371a3fSMartin Blumenstingl else
102965371a3fSMartin Blumenstingl rx_len = rtw_read32(rtwdev, REG_SDIO_RX0_REQ_LEN);
103065371a3fSMartin Blumenstingl
103165371a3fSMartin Blumenstingl if (!rx_len)
103265371a3fSMartin Blumenstingl break;
103365371a3fSMartin Blumenstingl
103465371a3fSMartin Blumenstingl rtw_sdio_rxfifo_recv(rtwdev, rx_len);
103565371a3fSMartin Blumenstingl
103665371a3fSMartin Blumenstingl total_rx_bytes += rx_len;
1037e967229eSMartin Blumenstingl
1038e967229eSMartin Blumenstingl if (rtw_chip_wcpu_11n(rtwdev)) {
1039e967229eSMartin Blumenstingl /* Stop if no more RX requests are pending, even if
1040e967229eSMartin Blumenstingl * rx_len could be greater than zero in the next
1041e967229eSMartin Blumenstingl * iteration. This is needed because the RX buffer may
1042e967229eSMartin Blumenstingl * already contain data while either HW or FW are not
1043e967229eSMartin Blumenstingl * done filling that buffer yet. Still reading the
1044e967229eSMartin Blumenstingl * buffer can result in packets where
1045e967229eSMartin Blumenstingl * rtw_rx_pkt_stat.pkt_len is zero or points beyond the
1046e967229eSMartin Blumenstingl * end of the buffer.
1047e967229eSMartin Blumenstingl */
1048e967229eSMartin Blumenstingl hisr = rtw_read32(rtwdev, REG_SDIO_HISR);
1049e967229eSMartin Blumenstingl } else {
1050e967229eSMartin Blumenstingl /* RTW_WCPU_11AC chips have improved hardware or
1051e967229eSMartin Blumenstingl * firmware and can use rx_len unconditionally.
1052e967229eSMartin Blumenstingl */
1053e967229eSMartin Blumenstingl hisr = REG_SDIO_HISR_RX_REQUEST;
105465371a3fSMartin Blumenstingl }
1055e967229eSMartin Blumenstingl } while (total_rx_bytes < SZ_64K && hisr & REG_SDIO_HISR_RX_REQUEST);
105665371a3fSMartin Blumenstingl }
105765371a3fSMartin Blumenstingl
rtw_sdio_handle_interrupt(struct sdio_func * sdio_func)105865371a3fSMartin Blumenstingl static void rtw_sdio_handle_interrupt(struct sdio_func *sdio_func)
105965371a3fSMartin Blumenstingl {
106065371a3fSMartin Blumenstingl struct ieee80211_hw *hw = sdio_get_drvdata(sdio_func);
106165371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio;
106265371a3fSMartin Blumenstingl struct rtw_dev *rtwdev;
106365371a3fSMartin Blumenstingl u32 hisr;
106465371a3fSMartin Blumenstingl
106565371a3fSMartin Blumenstingl rtwdev = hw->priv;
106665371a3fSMartin Blumenstingl rtwsdio = (struct rtw_sdio *)rtwdev->priv;
106765371a3fSMartin Blumenstingl
106865371a3fSMartin Blumenstingl rtwsdio->irq_thread = current;
106965371a3fSMartin Blumenstingl
107065371a3fSMartin Blumenstingl hisr = rtw_read32(rtwdev, REG_SDIO_HISR);
107165371a3fSMartin Blumenstingl
107265371a3fSMartin Blumenstingl if (hisr & REG_SDIO_HISR_TXERR)
107365371a3fSMartin Blumenstingl rtw_sdio_tx_err_isr(rtwdev);
107465371a3fSMartin Blumenstingl if (hisr & REG_SDIO_HISR_RX_REQUEST) {
107565371a3fSMartin Blumenstingl hisr &= ~REG_SDIO_HISR_RX_REQUEST;
107665371a3fSMartin Blumenstingl rtw_sdio_rx_isr(rtwdev);
107765371a3fSMartin Blumenstingl }
107865371a3fSMartin Blumenstingl
107965371a3fSMartin Blumenstingl rtw_write32(rtwdev, REG_SDIO_HISR, hisr);
108065371a3fSMartin Blumenstingl
108165371a3fSMartin Blumenstingl rtwsdio->irq_thread = NULL;
108265371a3fSMartin Blumenstingl }
108365371a3fSMartin Blumenstingl
rtw_sdio_suspend(struct device * dev)108465371a3fSMartin Blumenstingl static int __maybe_unused rtw_sdio_suspend(struct device *dev)
108565371a3fSMartin Blumenstingl {
108665371a3fSMartin Blumenstingl struct sdio_func *func = dev_to_sdio_func(dev);
108765371a3fSMartin Blumenstingl struct ieee80211_hw *hw = dev_get_drvdata(dev);
108865371a3fSMartin Blumenstingl struct rtw_dev *rtwdev = hw->priv;
108965371a3fSMartin Blumenstingl int ret;
109065371a3fSMartin Blumenstingl
109165371a3fSMartin Blumenstingl ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
109265371a3fSMartin Blumenstingl if (ret)
109365371a3fSMartin Blumenstingl rtw_err(rtwdev, "Failed to host PM flag MMC_PM_KEEP_POWER");
109465371a3fSMartin Blumenstingl
109565371a3fSMartin Blumenstingl return ret;
109665371a3fSMartin Blumenstingl }
109765371a3fSMartin Blumenstingl
rtw_sdio_resume(struct device * dev)109865371a3fSMartin Blumenstingl static int __maybe_unused rtw_sdio_resume(struct device *dev)
109965371a3fSMartin Blumenstingl {
110065371a3fSMartin Blumenstingl return 0;
110165371a3fSMartin Blumenstingl }
110265371a3fSMartin Blumenstingl
110365371a3fSMartin Blumenstingl SIMPLE_DEV_PM_OPS(rtw_sdio_pm_ops, rtw_sdio_suspend, rtw_sdio_resume);
110465371a3fSMartin Blumenstingl EXPORT_SYMBOL(rtw_sdio_pm_ops);
110565371a3fSMartin Blumenstingl
rtw_sdio_claim(struct rtw_dev * rtwdev,struct sdio_func * sdio_func)110665371a3fSMartin Blumenstingl static int rtw_sdio_claim(struct rtw_dev *rtwdev, struct sdio_func *sdio_func)
110765371a3fSMartin Blumenstingl {
110865371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
110965371a3fSMartin Blumenstingl int ret;
111065371a3fSMartin Blumenstingl
111165371a3fSMartin Blumenstingl sdio_claim_host(sdio_func);
111265371a3fSMartin Blumenstingl
111365371a3fSMartin Blumenstingl ret = sdio_enable_func(sdio_func);
111465371a3fSMartin Blumenstingl if (ret) {
111565371a3fSMartin Blumenstingl rtw_err(rtwdev, "Failed to enable SDIO func");
111665371a3fSMartin Blumenstingl goto err_release_host;
111765371a3fSMartin Blumenstingl }
111865371a3fSMartin Blumenstingl
111965371a3fSMartin Blumenstingl ret = sdio_set_block_size(sdio_func, RTW_SDIO_BLOCK_SIZE);
112065371a3fSMartin Blumenstingl if (ret) {
112165371a3fSMartin Blumenstingl rtw_err(rtwdev, "Failed to set SDIO block size to 512");
112265371a3fSMartin Blumenstingl goto err_disable_func;
112365371a3fSMartin Blumenstingl }
112465371a3fSMartin Blumenstingl
112565371a3fSMartin Blumenstingl rtwsdio->sdio_func = sdio_func;
112665371a3fSMartin Blumenstingl
112765371a3fSMartin Blumenstingl rtwsdio->sdio3_bus_mode = mmc_card_uhs(sdio_func->card);
112865371a3fSMartin Blumenstingl
112965371a3fSMartin Blumenstingl sdio_set_drvdata(sdio_func, rtwdev->hw);
113065371a3fSMartin Blumenstingl SET_IEEE80211_DEV(rtwdev->hw, &sdio_func->dev);
113165371a3fSMartin Blumenstingl
113265371a3fSMartin Blumenstingl sdio_release_host(sdio_func);
113365371a3fSMartin Blumenstingl
113465371a3fSMartin Blumenstingl return 0;
113565371a3fSMartin Blumenstingl
113665371a3fSMartin Blumenstingl err_disable_func:
113765371a3fSMartin Blumenstingl sdio_disable_func(sdio_func);
113865371a3fSMartin Blumenstingl err_release_host:
113965371a3fSMartin Blumenstingl sdio_release_host(sdio_func);
114065371a3fSMartin Blumenstingl return ret;
114165371a3fSMartin Blumenstingl }
114265371a3fSMartin Blumenstingl
rtw_sdio_declaim(struct rtw_dev * rtwdev,struct sdio_func * sdio_func)114365371a3fSMartin Blumenstingl static void rtw_sdio_declaim(struct rtw_dev *rtwdev,
114465371a3fSMartin Blumenstingl struct sdio_func *sdio_func)
114565371a3fSMartin Blumenstingl {
114665371a3fSMartin Blumenstingl sdio_claim_host(sdio_func);
114765371a3fSMartin Blumenstingl sdio_disable_func(sdio_func);
114865371a3fSMartin Blumenstingl sdio_release_host(sdio_func);
114965371a3fSMartin Blumenstingl }
115065371a3fSMartin Blumenstingl
115165371a3fSMartin Blumenstingl static struct rtw_hci_ops rtw_sdio_ops = {
115265371a3fSMartin Blumenstingl .tx_write = rtw_sdio_tx_write,
115365371a3fSMartin Blumenstingl .tx_kick_off = rtw_sdio_tx_kick_off,
115465371a3fSMartin Blumenstingl .setup = rtw_sdio_setup,
115565371a3fSMartin Blumenstingl .start = rtw_sdio_start,
115665371a3fSMartin Blumenstingl .stop = rtw_sdio_stop,
115765371a3fSMartin Blumenstingl .deep_ps = rtw_sdio_deep_ps,
115865371a3fSMartin Blumenstingl .link_ps = rtw_sdio_link_ps,
115965371a3fSMartin Blumenstingl .interface_cfg = rtw_sdio_interface_cfg,
1160*002a5db9SBitterblue Smith .dynamic_rx_agg = NULL,
116165371a3fSMartin Blumenstingl
116265371a3fSMartin Blumenstingl .read8 = rtw_sdio_read8,
116365371a3fSMartin Blumenstingl .read16 = rtw_sdio_read16,
116465371a3fSMartin Blumenstingl .read32 = rtw_sdio_read32,
116565371a3fSMartin Blumenstingl .write8 = rtw_sdio_write8,
116665371a3fSMartin Blumenstingl .write16 = rtw_sdio_write16,
116765371a3fSMartin Blumenstingl .write32 = rtw_sdio_write32,
116865371a3fSMartin Blumenstingl .write_data_rsvd_page = rtw_sdio_write_data_rsvd_page,
116965371a3fSMartin Blumenstingl .write_data_h2c = rtw_sdio_write_data_h2c,
117065371a3fSMartin Blumenstingl };
117165371a3fSMartin Blumenstingl
rtw_sdio_request_irq(struct rtw_dev * rtwdev,struct sdio_func * sdio_func)117265371a3fSMartin Blumenstingl static int rtw_sdio_request_irq(struct rtw_dev *rtwdev,
117365371a3fSMartin Blumenstingl struct sdio_func *sdio_func)
117465371a3fSMartin Blumenstingl {
117565371a3fSMartin Blumenstingl int ret;
117665371a3fSMartin Blumenstingl
117765371a3fSMartin Blumenstingl sdio_claim_host(sdio_func);
117865371a3fSMartin Blumenstingl ret = sdio_claim_irq(sdio_func, &rtw_sdio_handle_interrupt);
117965371a3fSMartin Blumenstingl sdio_release_host(sdio_func);
118065371a3fSMartin Blumenstingl
118165371a3fSMartin Blumenstingl if (ret) {
118265371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to claim SDIO IRQ");
118365371a3fSMartin Blumenstingl return ret;
118465371a3fSMartin Blumenstingl }
118565371a3fSMartin Blumenstingl
118665371a3fSMartin Blumenstingl return 0;
118765371a3fSMartin Blumenstingl }
118865371a3fSMartin Blumenstingl
rtw_sdio_indicate_tx_status(struct rtw_dev * rtwdev,struct sk_buff * skb)118965371a3fSMartin Blumenstingl static void rtw_sdio_indicate_tx_status(struct rtw_dev *rtwdev,
119065371a3fSMartin Blumenstingl struct sk_buff *skb)
119165371a3fSMartin Blumenstingl {
119265371a3fSMartin Blumenstingl struct rtw_sdio_tx_data *tx_data = rtw_sdio_get_tx_data(skb);
119365371a3fSMartin Blumenstingl struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
119465371a3fSMartin Blumenstingl struct ieee80211_hw *hw = rtwdev->hw;
119565371a3fSMartin Blumenstingl
119665371a3fSMartin Blumenstingl /* enqueue to wait for tx report */
119765371a3fSMartin Blumenstingl if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) {
119865371a3fSMartin Blumenstingl rtw_tx_report_enqueue(rtwdev, skb, tx_data->sn);
119965371a3fSMartin Blumenstingl return;
120065371a3fSMartin Blumenstingl }
120165371a3fSMartin Blumenstingl
120265371a3fSMartin Blumenstingl /* always ACK for others, then they won't be marked as drop */
120365371a3fSMartin Blumenstingl ieee80211_tx_info_clear_status(info);
120465371a3fSMartin Blumenstingl if (info->flags & IEEE80211_TX_CTL_NO_ACK)
120565371a3fSMartin Blumenstingl info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
120665371a3fSMartin Blumenstingl else
120765371a3fSMartin Blumenstingl info->flags |= IEEE80211_TX_STAT_ACK;
120865371a3fSMartin Blumenstingl
120965371a3fSMartin Blumenstingl ieee80211_tx_status_irqsafe(hw, skb);
121065371a3fSMartin Blumenstingl }
121165371a3fSMartin Blumenstingl
rtw_sdio_process_tx_queue(struct rtw_dev * rtwdev,enum rtw_tx_queue_type queue)121265371a3fSMartin Blumenstingl static void rtw_sdio_process_tx_queue(struct rtw_dev *rtwdev,
121365371a3fSMartin Blumenstingl enum rtw_tx_queue_type queue)
121465371a3fSMartin Blumenstingl {
121565371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
121665371a3fSMartin Blumenstingl struct sk_buff *skb;
121765371a3fSMartin Blumenstingl int ret;
121865371a3fSMartin Blumenstingl
121965371a3fSMartin Blumenstingl skb = skb_dequeue(&rtwsdio->tx_queue[queue]);
122065371a3fSMartin Blumenstingl if (!skb)
122165371a3fSMartin Blumenstingl return;
122265371a3fSMartin Blumenstingl
122365371a3fSMartin Blumenstingl ret = rtw_sdio_write_port(rtwdev, skb, queue);
122465371a3fSMartin Blumenstingl if (ret) {
122565371a3fSMartin Blumenstingl skb_queue_head(&rtwsdio->tx_queue[queue], skb);
122665371a3fSMartin Blumenstingl return;
122765371a3fSMartin Blumenstingl }
122865371a3fSMartin Blumenstingl
122965371a3fSMartin Blumenstingl if (queue <= RTW_TX_QUEUE_VO)
123065371a3fSMartin Blumenstingl rtw_sdio_indicate_tx_status(rtwdev, skb);
123165371a3fSMartin Blumenstingl else
123265371a3fSMartin Blumenstingl dev_kfree_skb_any(skb);
123365371a3fSMartin Blumenstingl }
123465371a3fSMartin Blumenstingl
rtw_sdio_tx_handler(struct work_struct * work)123565371a3fSMartin Blumenstingl static void rtw_sdio_tx_handler(struct work_struct *work)
123665371a3fSMartin Blumenstingl {
123765371a3fSMartin Blumenstingl struct rtw_sdio_work_data *work_data =
123865371a3fSMartin Blumenstingl container_of(work, struct rtw_sdio_work_data, work);
123965371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio;
124065371a3fSMartin Blumenstingl struct rtw_dev *rtwdev;
124165371a3fSMartin Blumenstingl int limit, queue;
124265371a3fSMartin Blumenstingl
124365371a3fSMartin Blumenstingl rtwdev = work_data->rtwdev;
124465371a3fSMartin Blumenstingl rtwsdio = (struct rtw_sdio *)rtwdev->priv;
124565371a3fSMartin Blumenstingl
124665371a3fSMartin Blumenstingl if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_TX_WAKE))
124765371a3fSMartin Blumenstingl rtw_sdio_deep_ps_leave(rtwdev);
124865371a3fSMartin Blumenstingl
124965371a3fSMartin Blumenstingl for (queue = RTK_MAX_TX_QUEUE_NUM - 1; queue >= 0; queue--) {
125065371a3fSMartin Blumenstingl for (limit = 0; limit < 1000; limit++) {
125165371a3fSMartin Blumenstingl rtw_sdio_process_tx_queue(rtwdev, queue);
125265371a3fSMartin Blumenstingl
125365371a3fSMartin Blumenstingl if (skb_queue_empty(&rtwsdio->tx_queue[queue]))
125465371a3fSMartin Blumenstingl break;
125565371a3fSMartin Blumenstingl }
125665371a3fSMartin Blumenstingl }
125765371a3fSMartin Blumenstingl }
125865371a3fSMartin Blumenstingl
rtw_sdio_free_irq(struct rtw_dev * rtwdev,struct sdio_func * sdio_func)125965371a3fSMartin Blumenstingl static void rtw_sdio_free_irq(struct rtw_dev *rtwdev,
126065371a3fSMartin Blumenstingl struct sdio_func *sdio_func)
126165371a3fSMartin Blumenstingl {
126265371a3fSMartin Blumenstingl sdio_claim_host(sdio_func);
126365371a3fSMartin Blumenstingl sdio_release_irq(sdio_func);
126465371a3fSMartin Blumenstingl sdio_release_host(sdio_func);
126565371a3fSMartin Blumenstingl }
126665371a3fSMartin Blumenstingl
rtw_sdio_init_tx(struct rtw_dev * rtwdev)126765371a3fSMartin Blumenstingl static int rtw_sdio_init_tx(struct rtw_dev *rtwdev)
126865371a3fSMartin Blumenstingl {
126965371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
127065371a3fSMartin Blumenstingl int i;
127165371a3fSMartin Blumenstingl
127265371a3fSMartin Blumenstingl rtwsdio->txwq = create_singlethread_workqueue("rtw88_sdio: tx wq");
127365371a3fSMartin Blumenstingl if (!rtwsdio->txwq) {
127465371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to create TX work queue\n");
127565371a3fSMartin Blumenstingl return -ENOMEM;
127665371a3fSMartin Blumenstingl }
127765371a3fSMartin Blumenstingl
127865371a3fSMartin Blumenstingl for (i = 0; i < RTK_MAX_TX_QUEUE_NUM; i++)
127965371a3fSMartin Blumenstingl skb_queue_head_init(&rtwsdio->tx_queue[i]);
128065371a3fSMartin Blumenstingl rtwsdio->tx_handler_data = kmalloc(sizeof(*rtwsdio->tx_handler_data),
128165371a3fSMartin Blumenstingl GFP_KERNEL);
128265371a3fSMartin Blumenstingl if (!rtwsdio->tx_handler_data)
128365371a3fSMartin Blumenstingl goto err_destroy_wq;
128465371a3fSMartin Blumenstingl
128565371a3fSMartin Blumenstingl rtwsdio->tx_handler_data->rtwdev = rtwdev;
128665371a3fSMartin Blumenstingl INIT_WORK(&rtwsdio->tx_handler_data->work, rtw_sdio_tx_handler);
128765371a3fSMartin Blumenstingl
128865371a3fSMartin Blumenstingl return 0;
128965371a3fSMartin Blumenstingl
129065371a3fSMartin Blumenstingl err_destroy_wq:
129165371a3fSMartin Blumenstingl destroy_workqueue(rtwsdio->txwq);
129265371a3fSMartin Blumenstingl return -ENOMEM;
129365371a3fSMartin Blumenstingl }
129465371a3fSMartin Blumenstingl
rtw_sdio_deinit_tx(struct rtw_dev * rtwdev)129565371a3fSMartin Blumenstingl static void rtw_sdio_deinit_tx(struct rtw_dev *rtwdev)
129665371a3fSMartin Blumenstingl {
129765371a3fSMartin Blumenstingl struct rtw_sdio *rtwsdio = (struct rtw_sdio *)rtwdev->priv;
129865371a3fSMartin Blumenstingl int i;
129965371a3fSMartin Blumenstingl
130065371a3fSMartin Blumenstingl for (i = 0; i < RTK_MAX_TX_QUEUE_NUM; i++)
130165371a3fSMartin Blumenstingl skb_queue_purge(&rtwsdio->tx_queue[i]);
130265371a3fSMartin Blumenstingl
130365371a3fSMartin Blumenstingl flush_workqueue(rtwsdio->txwq);
130465371a3fSMartin Blumenstingl destroy_workqueue(rtwsdio->txwq);
130565371a3fSMartin Blumenstingl kfree(rtwsdio->tx_handler_data);
130665371a3fSMartin Blumenstingl }
130765371a3fSMartin Blumenstingl
rtw_sdio_probe(struct sdio_func * sdio_func,const struct sdio_device_id * id)130865371a3fSMartin Blumenstingl int rtw_sdio_probe(struct sdio_func *sdio_func,
130965371a3fSMartin Blumenstingl const struct sdio_device_id *id)
131065371a3fSMartin Blumenstingl {
131165371a3fSMartin Blumenstingl struct ieee80211_hw *hw;
131265371a3fSMartin Blumenstingl struct rtw_dev *rtwdev;
131365371a3fSMartin Blumenstingl int drv_data_size;
131465371a3fSMartin Blumenstingl int ret;
131565371a3fSMartin Blumenstingl
131665371a3fSMartin Blumenstingl drv_data_size = sizeof(struct rtw_dev) + sizeof(struct rtw_sdio);
131765371a3fSMartin Blumenstingl hw = ieee80211_alloc_hw(drv_data_size, &rtw_ops);
131865371a3fSMartin Blumenstingl if (!hw) {
131965371a3fSMartin Blumenstingl dev_err(&sdio_func->dev, "failed to allocate hw");
132065371a3fSMartin Blumenstingl return -ENOMEM;
132165371a3fSMartin Blumenstingl }
132265371a3fSMartin Blumenstingl
132365371a3fSMartin Blumenstingl rtwdev = hw->priv;
132465371a3fSMartin Blumenstingl rtwdev->hw = hw;
132565371a3fSMartin Blumenstingl rtwdev->dev = &sdio_func->dev;
132665371a3fSMartin Blumenstingl rtwdev->chip = (struct rtw_chip_info *)id->driver_data;
132765371a3fSMartin Blumenstingl rtwdev->hci.ops = &rtw_sdio_ops;
132865371a3fSMartin Blumenstingl rtwdev->hci.type = RTW_HCI_TYPE_SDIO;
132965371a3fSMartin Blumenstingl
133065371a3fSMartin Blumenstingl ret = rtw_core_init(rtwdev);
133165371a3fSMartin Blumenstingl if (ret)
133265371a3fSMartin Blumenstingl goto err_release_hw;
133365371a3fSMartin Blumenstingl
133465371a3fSMartin Blumenstingl rtw_dbg(rtwdev, RTW_DBG_SDIO,
133565371a3fSMartin Blumenstingl "rtw88 SDIO probe: vendor=0x%04x device=%04x class=%02x",
133665371a3fSMartin Blumenstingl id->vendor, id->device, id->class);
133765371a3fSMartin Blumenstingl
133865371a3fSMartin Blumenstingl ret = rtw_sdio_claim(rtwdev, sdio_func);
133965371a3fSMartin Blumenstingl if (ret) {
134065371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to claim SDIO device");
134165371a3fSMartin Blumenstingl goto err_deinit_core;
134265371a3fSMartin Blumenstingl }
134365371a3fSMartin Blumenstingl
134465371a3fSMartin Blumenstingl rtw_sdio_init(rtwdev);
134565371a3fSMartin Blumenstingl
134665371a3fSMartin Blumenstingl ret = rtw_sdio_init_tx(rtwdev);
134765371a3fSMartin Blumenstingl if (ret) {
134865371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to init SDIO TX queue\n");
134965371a3fSMartin Blumenstingl goto err_sdio_declaim;
135065371a3fSMartin Blumenstingl }
135165371a3fSMartin Blumenstingl
135265371a3fSMartin Blumenstingl ret = rtw_chip_info_setup(rtwdev);
135365371a3fSMartin Blumenstingl if (ret) {
135465371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to setup chip information");
135565371a3fSMartin Blumenstingl goto err_destroy_txwq;
135665371a3fSMartin Blumenstingl }
135765371a3fSMartin Blumenstingl
135865371a3fSMartin Blumenstingl ret = rtw_sdio_request_irq(rtwdev, sdio_func);
135965371a3fSMartin Blumenstingl if (ret)
136065371a3fSMartin Blumenstingl goto err_destroy_txwq;
136165371a3fSMartin Blumenstingl
136265371a3fSMartin Blumenstingl ret = rtw_register_hw(rtwdev, hw);
136365371a3fSMartin Blumenstingl if (ret) {
136465371a3fSMartin Blumenstingl rtw_err(rtwdev, "failed to register hw");
136565371a3fSMartin Blumenstingl goto err_free_irq;
136665371a3fSMartin Blumenstingl }
136765371a3fSMartin Blumenstingl
136865371a3fSMartin Blumenstingl return 0;
136965371a3fSMartin Blumenstingl
137065371a3fSMartin Blumenstingl err_free_irq:
137165371a3fSMartin Blumenstingl rtw_sdio_free_irq(rtwdev, sdio_func);
137265371a3fSMartin Blumenstingl err_destroy_txwq:
137365371a3fSMartin Blumenstingl rtw_sdio_deinit_tx(rtwdev);
137465371a3fSMartin Blumenstingl err_sdio_declaim:
137565371a3fSMartin Blumenstingl rtw_sdio_declaim(rtwdev, sdio_func);
137665371a3fSMartin Blumenstingl err_deinit_core:
137765371a3fSMartin Blumenstingl rtw_core_deinit(rtwdev);
137865371a3fSMartin Blumenstingl err_release_hw:
137965371a3fSMartin Blumenstingl ieee80211_free_hw(hw);
138065371a3fSMartin Blumenstingl
138165371a3fSMartin Blumenstingl return ret;
138265371a3fSMartin Blumenstingl }
138365371a3fSMartin Blumenstingl EXPORT_SYMBOL(rtw_sdio_probe);
138465371a3fSMartin Blumenstingl
rtw_sdio_remove(struct sdio_func * sdio_func)138565371a3fSMartin Blumenstingl void rtw_sdio_remove(struct sdio_func *sdio_func)
138665371a3fSMartin Blumenstingl {
138765371a3fSMartin Blumenstingl struct ieee80211_hw *hw = sdio_get_drvdata(sdio_func);
138865371a3fSMartin Blumenstingl struct rtw_dev *rtwdev;
138965371a3fSMartin Blumenstingl
139065371a3fSMartin Blumenstingl if (!hw)
139165371a3fSMartin Blumenstingl return;
139265371a3fSMartin Blumenstingl
139365371a3fSMartin Blumenstingl rtwdev = hw->priv;
139465371a3fSMartin Blumenstingl
139565371a3fSMartin Blumenstingl rtw_unregister_hw(rtwdev, hw);
139665371a3fSMartin Blumenstingl rtw_sdio_disable_interrupt(rtwdev);
139765371a3fSMartin Blumenstingl rtw_sdio_free_irq(rtwdev, sdio_func);
139865371a3fSMartin Blumenstingl rtw_sdio_declaim(rtwdev, sdio_func);
139965371a3fSMartin Blumenstingl rtw_sdio_deinit_tx(rtwdev);
140065371a3fSMartin Blumenstingl rtw_core_deinit(rtwdev);
140165371a3fSMartin Blumenstingl ieee80211_free_hw(hw);
140265371a3fSMartin Blumenstingl }
140365371a3fSMartin Blumenstingl EXPORT_SYMBOL(rtw_sdio_remove);
140465371a3fSMartin Blumenstingl
rtw_sdio_shutdown(struct device * dev)140565371a3fSMartin Blumenstingl void rtw_sdio_shutdown(struct device *dev)
140665371a3fSMartin Blumenstingl {
140765371a3fSMartin Blumenstingl struct sdio_func *sdio_func = dev_to_sdio_func(dev);
140865371a3fSMartin Blumenstingl const struct rtw_chip_info *chip;
140965371a3fSMartin Blumenstingl struct ieee80211_hw *hw;
141065371a3fSMartin Blumenstingl struct rtw_dev *rtwdev;
141165371a3fSMartin Blumenstingl
141265371a3fSMartin Blumenstingl hw = sdio_get_drvdata(sdio_func);
141365371a3fSMartin Blumenstingl if (!hw)
141465371a3fSMartin Blumenstingl return;
141565371a3fSMartin Blumenstingl
141665371a3fSMartin Blumenstingl rtwdev = hw->priv;
141765371a3fSMartin Blumenstingl chip = rtwdev->chip;
141865371a3fSMartin Blumenstingl
141965371a3fSMartin Blumenstingl if (chip->ops->shutdown)
142065371a3fSMartin Blumenstingl chip->ops->shutdown(rtwdev);
142165371a3fSMartin Blumenstingl }
142265371a3fSMartin Blumenstingl EXPORT_SYMBOL(rtw_sdio_shutdown);
142365371a3fSMartin Blumenstingl
142465371a3fSMartin Blumenstingl MODULE_AUTHOR("Martin Blumenstingl");
142565371a3fSMartin Blumenstingl MODULE_AUTHOR("Jernej Skrabec");
142665371a3fSMartin Blumenstingl MODULE_DESCRIPTION("Realtek 802.11ac wireless SDIO driver");
142765371a3fSMartin Blumenstingl MODULE_LICENSE("Dual BSD/GPL");
1428