2019-09-19 14:25:36 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* SDIO interface.
|
|
|
|
*
|
2020-10-07 12:19:41 +02:00
|
|
|
* Copyright (c) 2017-2020, Silicon Laboratories, Inc.
|
2019-09-19 14:25:36 +00:00
|
|
|
* Copyright (c) 2010, ST-Ericsson
|
|
|
|
*/
|
2021-03-09 15:51:56 +01:00
|
|
|
#include <linux/module.h>
|
2020-05-05 14:37:47 +02:00
|
|
|
#include <linux/mmc/sdio.h>
|
2019-09-19 14:25:36 +00:00
|
|
|
#include <linux/mmc/sdio_func.h>
|
|
|
|
#include <linux/mmc/card.h>
|
2021-03-09 15:51:56 +01:00
|
|
|
#include <linux/interrupt.h>
|
2023-07-24 15:19:13 -06:00
|
|
|
#include <linux/of.h>
|
2019-09-19 14:25:36 +00:00
|
|
|
#include <linux/of_irq.h>
|
2021-03-09 15:51:56 +01:00
|
|
|
#include <linux/irq.h>
|
2022-01-13 09:55:01 +01:00
|
|
|
#include <linux/align.h>
|
2025-03-04 16:32:23 +01:00
|
|
|
#include <linux/pm.h>
|
2019-09-19 14:25:36 +00:00
|
|
|
|
|
|
|
#include "bus.h"
|
2019-09-19 14:25:37 +00:00
|
|
|
#include "wfx.h"
|
2021-03-09 15:51:56 +01:00
|
|
|
#include "hwio.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "bh.h"
|
2019-09-19 14:25:37 +00:00
|
|
|
|
2022-01-13 09:55:20 +01:00
|
|
|
static const struct wfx_platform_data pdata_wf200 = {
|
2022-01-13 09:55:21 +01:00
|
|
|
.file_fw = "wfx/wfm_wf200",
|
|
|
|
.file_pds = "wfx/wf200.pds",
|
2022-01-13 09:55:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct wfx_platform_data pdata_brd4001a = {
|
2022-01-13 09:55:21 +01:00
|
|
|
.file_fw = "wfx/wfm_wf200",
|
|
|
|
.file_pds = "wfx/brd4001a.pds",
|
2022-01-13 09:55:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct wfx_platform_data pdata_brd8022a = {
|
2022-01-13 09:55:21 +01:00
|
|
|
.file_fw = "wfx/wfm_wf200",
|
|
|
|
.file_pds = "wfx/brd8022a.pds",
|
2022-01-13 09:55:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct wfx_platform_data pdata_brd8023a = {
|
2022-01-13 09:55:21 +01:00
|
|
|
.file_fw = "wfx/wfm_wf200",
|
|
|
|
.file_pds = "wfx/brd8023a.pds",
|
2022-01-13 09:55:20 +01:00
|
|
|
};
|
|
|
|
|
2019-09-19 14:25:37 +00:00
|
|
|
struct wfx_sdio_priv {
|
|
|
|
struct sdio_func *func;
|
|
|
|
struct wfx_dev *core;
|
|
|
|
u8 buf_id_tx;
|
|
|
|
u8 buf_id_rx;
|
|
|
|
int of_irq;
|
|
|
|
};
|
|
|
|
|
2022-01-13 09:55:13 +01:00
|
|
|
static int wfx_sdio_copy_from_io(void *priv, unsigned int reg_id, void *dst, size_t count)
|
2019-09-19 14:25:37 +00:00
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
unsigned int sdio_addr = reg_id << 2;
|
|
|
|
int ret;
|
|
|
|
|
2019-10-08 09:43:01 +00:00
|
|
|
WARN(reg_id > 7, "chip only has 7 registers");
|
2022-01-13 09:55:01 +01:00
|
|
|
WARN(!IS_ALIGNED((uintptr_t)dst, 4), "unaligned buffer address");
|
|
|
|
WARN(!IS_ALIGNED(count, 4), "unaligned buffer size");
|
2019-09-19 14:25:37 +00:00
|
|
|
|
|
|
|
/* Use queue mode buffers */
|
|
|
|
if (reg_id == WFX_REG_IN_OUT_QUEUE)
|
|
|
|
sdio_addr |= (bus->buf_id_rx + 1) << 7;
|
|
|
|
ret = sdio_memcpy_fromio(bus->func, dst, sdio_addr, count);
|
|
|
|
if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
|
|
|
|
bus->buf_id_rx = (bus->buf_id_rx + 1) % 4;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-13 09:55:13 +01:00
|
|
|
static int wfx_sdio_copy_to_io(void *priv, unsigned int reg_id, const void *src, size_t count)
|
2019-09-19 14:25:37 +00:00
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
unsigned int sdio_addr = reg_id << 2;
|
|
|
|
int ret;
|
|
|
|
|
2019-10-08 09:43:01 +00:00
|
|
|
WARN(reg_id > 7, "chip only has 7 registers");
|
2022-01-13 09:55:01 +01:00
|
|
|
WARN(!IS_ALIGNED((uintptr_t)src, 4), "unaligned buffer address");
|
|
|
|
WARN(!IS_ALIGNED(count, 4), "unaligned buffer size");
|
2019-09-19 14:25:37 +00:00
|
|
|
|
|
|
|
/* Use queue mode buffers */
|
|
|
|
if (reg_id == WFX_REG_IN_OUT_QUEUE)
|
|
|
|
sdio_addr |= bus->buf_id_tx << 7;
|
2021-09-13 15:01:58 +02:00
|
|
|
/* FIXME: discards 'const' qualifier for src */
|
2019-10-19 15:07:15 +01:00
|
|
|
ret = sdio_memcpy_toio(bus->func, sdio_addr, (void *)src, count);
|
2019-09-19 14:25:37 +00:00
|
|
|
if (!ret && reg_id == WFX_REG_IN_OUT_QUEUE)
|
|
|
|
bus->buf_id_tx = (bus->buf_id_tx + 1) % 32;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wfx_sdio_lock(void *priv)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
|
|
|
|
sdio_claim_host(bus->func);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wfx_sdio_unlock(void *priv)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
|
|
|
|
sdio_release_host(bus->func);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wfx_sdio_irq_handler(struct sdio_func *func)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
|
|
|
|
|
2020-05-05 14:37:46 +02:00
|
|
|
wfx_bh_request_rx(bus->core);
|
2019-09-19 14:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t wfx_sdio_irq_handler_ext(int irq, void *priv)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
|
|
|
|
sdio_claim_host(bus->func);
|
staging: wfx: add IRQ handling
bh_work() is in charge to schedule all HIF message from/to chip.
On normal operation, when an IRQ is received, driver can get size of
next message in control register. In order to save control register
access, when chip send a message, it also appends a copy of control
register after the message (this register is not accounted in message
length declared in message header, but must accounted in bus request).
This copy of control register is called "piggyback".
It also handles a power saving mechanism specific to WFxxx series. This
mechanism is based on a GPIO called "wakeup" GPIO. Obviously, this gpio
is not part of SPI/SDIO standard buses and must be declared
independently (this is the main reason for why SDIO mode try to get
parameters from DT).
When wakeup is enabled, host can communicate with chip only if it is
awake. To wake up chip, there are two cases:
- host receive an IRQ from chip (chip initiate communication): host
just have to set wakeup GPIO before reading data
- host want to send data to chip: host set wakeup GPIO, then wait
for an IRQ (in fact, wait for an empty message) and finally send data
bh_work() is also in charge to track usage of chip buffers. Normally
each request expect a confirmation. However, you can notice that special
"multi tx" confirmation can acknowledge multiple requests at time.
Finally, note that wfx_bh_request_rx() is not atomic (because of
control_reg_read()). So, in SPI mode, hard-irq handler only postpone all
processing to wfx_spi_request_rx().
Signed-off-by: Jérôme Pouiller <jerome.pouiller@silabs.com>
Link: https://lore.kernel.org/r/20190919142527.31797-8-Jerome.Pouiller@silabs.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-09-19 14:25:40 +00:00
|
|
|
wfx_bh_request_rx(bus->core);
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_release_host(bus->func);
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2020-05-05 14:37:50 +02:00
|
|
|
static int wfx_sdio_irq_subscribe(void *priv)
|
2019-09-19 14:25:37 +00:00
|
|
|
{
|
2020-05-05 14:37:50 +02:00
|
|
|
struct wfx_sdio_priv *bus = priv;
|
2020-05-05 14:37:47 +02:00
|
|
|
u32 flags;
|
2019-09-19 14:25:37 +00:00
|
|
|
int ret;
|
2020-05-05 14:37:47 +02:00
|
|
|
u8 cccr;
|
2019-09-19 14:25:37 +00:00
|
|
|
|
2020-05-05 14:37:47 +02:00
|
|
|
if (!bus->of_irq) {
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_claim_host(bus->func);
|
|
|
|
ret = sdio_claim_irq(bus->func, wfx_sdio_irq_handler);
|
|
|
|
sdio_release_host(bus->func);
|
2020-05-05 14:37:47 +02:00
|
|
|
return ret;
|
2019-09-19 14:25:37 +00:00
|
|
|
}
|
2020-05-05 14:37:47 +02:00
|
|
|
|
2021-09-13 15:02:03 +02:00
|
|
|
flags = irq_get_trigger_type(bus->of_irq);
|
|
|
|
if (!flags)
|
|
|
|
flags = IRQF_TRIGGER_HIGH;
|
|
|
|
flags |= IRQF_ONESHOT;
|
|
|
|
ret = devm_request_threaded_irq(&bus->func->dev, bus->of_irq, NULL,
|
2022-01-13 09:55:13 +01:00
|
|
|
wfx_sdio_irq_handler_ext, flags, "wfx", bus);
|
2021-09-13 15:02:03 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-05-05 14:37:47 +02:00
|
|
|
sdio_claim_host(bus->func);
|
|
|
|
cccr = sdio_f0_readb(bus->func, SDIO_CCCR_IENx, NULL);
|
|
|
|
cccr |= BIT(0);
|
|
|
|
cccr |= BIT(bus->func->num);
|
|
|
|
sdio_f0_writeb(bus->func, cccr, SDIO_CCCR_IENx, NULL);
|
|
|
|
sdio_release_host(bus->func);
|
2021-09-13 15:02:03 +02:00
|
|
|
return 0;
|
2019-09-19 14:25:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 14:37:50 +02:00
|
|
|
static int wfx_sdio_irq_unsubscribe(void *priv)
|
2019-09-19 14:25:37 +00:00
|
|
|
{
|
2020-05-05 14:37:50 +02:00
|
|
|
struct wfx_sdio_priv *bus = priv;
|
2019-09-19 14:25:37 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-05-05 14:37:47 +02:00
|
|
|
if (bus->of_irq)
|
|
|
|
devm_free_irq(&bus->func->dev, bus->of_irq, bus);
|
|
|
|
sdio_claim_host(bus->func);
|
|
|
|
ret = sdio_release_irq(bus->func);
|
|
|
|
sdio_release_host(bus->func);
|
2019-09-19 14:25:37 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t wfx_sdio_align_size(void *priv, size_t size)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
|
|
|
|
return sdio_align_size(bus->func, size);
|
|
|
|
}
|
|
|
|
|
2025-03-04 16:32:24 +01:00
|
|
|
static void wfx_sdio_set_wakeup(void *priv, bool enabled)
|
|
|
|
{
|
|
|
|
struct wfx_sdio_priv *bus = priv;
|
|
|
|
|
|
|
|
device_set_wakeup_enable(&bus->func->dev, enabled);
|
|
|
|
}
|
|
|
|
|
2022-01-13 09:55:12 +01:00
|
|
|
static const struct wfx_hwbus_ops wfx_sdio_hwbus_ops = {
|
2022-01-13 09:55:15 +01:00
|
|
|
.copy_from_io = wfx_sdio_copy_from_io,
|
|
|
|
.copy_to_io = wfx_sdio_copy_to_io,
|
|
|
|
.irq_subscribe = wfx_sdio_irq_subscribe,
|
2020-05-05 14:37:50 +02:00
|
|
|
.irq_unsubscribe = wfx_sdio_irq_unsubscribe,
|
2022-01-13 09:55:15 +01:00
|
|
|
.lock = wfx_sdio_lock,
|
|
|
|
.unlock = wfx_sdio_unlock,
|
|
|
|
.align_size = wfx_sdio_align_size,
|
2025-03-04 16:32:24 +01:00
|
|
|
.set_wakeup = wfx_sdio_set_wakeup,
|
2019-09-19 14:25:37 +00:00
|
|
|
};
|
2019-09-19 14:25:36 +00:00
|
|
|
|
2020-04-29 16:21:09 +02:00
|
|
|
static const struct of_device_id wfx_sdio_of_match[] = {
|
2022-01-13 09:55:20 +01:00
|
|
|
{ .compatible = "silabs,wf200", .data = &pdata_wf200 },
|
|
|
|
{ .compatible = "silabs,brd4001a", .data = &pdata_brd4001a },
|
|
|
|
{ .compatible = "silabs,brd8022a", .data = &pdata_brd8022a },
|
|
|
|
{ .compatible = "silabs,brd8023a", .data = &pdata_brd8023a },
|
2020-04-29 16:21:09 +02:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, wfx_sdio_of_match);
|
|
|
|
|
2025-03-04 16:32:23 +01:00
|
|
|
static int wfx_sdio_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdio_func *func = dev_to_sdio_func(dev);
|
|
|
|
struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!device_may_wakeup(dev))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
flush_work(&bus->core->hif.bh);
|
|
|
|
/* Either "wakeup-source" attribute or out-of-band IRQ is required for
|
|
|
|
* WoWLAN
|
|
|
|
*/
|
|
|
|
if (bus->of_irq) {
|
|
|
|
ret = enable_irq_wake(bus->of_irq);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
ret = sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wfx_sdio_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdio_func *func = dev_to_sdio_func(dev);
|
|
|
|
struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
|
|
|
|
|
|
|
|
if (!device_may_wakeup(dev))
|
|
|
|
return 0;
|
|
|
|
if (bus->of_irq)
|
|
|
|
return disable_irq_wake(bus->of_irq);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-13 09:55:13 +01:00
|
|
|
static int wfx_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
|
2019-09-19 14:25:36 +00:00
|
|
|
{
|
2022-01-13 09:55:20 +01:00
|
|
|
const struct wfx_platform_data *pdata = of_device_get_match_data(&func->dev);
|
2025-03-04 16:32:23 +01:00
|
|
|
mmc_pm_flag_t pm_flag = sdio_get_host_pm_caps(func);
|
2019-09-19 14:25:36 +00:00
|
|
|
struct device_node *np = func->dev.of_node;
|
2019-09-19 14:25:37 +00:00
|
|
|
struct wfx_sdio_priv *bus;
|
|
|
|
int ret;
|
2019-09-19 14:25:36 +00:00
|
|
|
|
|
|
|
if (func->num != 1) {
|
2020-05-15 10:33:08 +02:00
|
|
|
dev_err(&func->dev, "SDIO function number is %d while it should always be 1 (unsupported chip?)\n",
|
|
|
|
func->num);
|
2019-09-19 14:25:36 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-01-13 09:55:24 +01:00
|
|
|
if (!pdata) {
|
|
|
|
dev_warn(&func->dev, "no compatible device found in DT\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:25:37 +00:00
|
|
|
bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
|
|
|
|
if (!bus)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
bus->func = func;
|
2022-01-13 09:55:24 +01:00
|
|
|
bus->of_irq = irq_of_parse_and_map(np, 0);
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_set_drvdata(func, bus);
|
|
|
|
|
|
|
|
sdio_claim_host(func);
|
|
|
|
ret = sdio_enable_func(func);
|
2021-09-13 15:01:58 +02:00
|
|
|
/* Block of 64 bytes is more efficient than 512B for frame sizes < 4k */
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_set_block_size(func, 64);
|
|
|
|
sdio_release_host(func);
|
|
|
|
if (ret)
|
2022-01-13 09:55:16 +01:00
|
|
|
return ret;
|
2019-09-19 14:25:37 +00:00
|
|
|
|
2022-01-13 09:55:20 +01:00
|
|
|
bus->core = wfx_init_common(&func->dev, pdata, &wfx_sdio_hwbus_ops, bus);
|
2019-09-19 14:25:37 +00:00
|
|
|
if (!bus->core) {
|
|
|
|
ret = -EIO;
|
2022-01-13 09:55:16 +01:00
|
|
|
goto sdio_release;
|
2019-09-19 14:25:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 14:25:38 +00:00
|
|
|
ret = wfx_probe(bus->core);
|
|
|
|
if (ret)
|
2022-01-13 09:55:16 +01:00
|
|
|
goto sdio_release;
|
2019-09-19 14:25:38 +00:00
|
|
|
|
2025-03-04 16:32:23 +01:00
|
|
|
if (pm_flag & MMC_PM_KEEP_POWER)
|
|
|
|
device_set_wakeup_capable(&func->dev, true);
|
|
|
|
|
2019-09-19 14:25:37 +00:00
|
|
|
return 0;
|
|
|
|
|
2022-01-13 09:55:16 +01:00
|
|
|
sdio_release:
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_claim_host(func);
|
|
|
|
sdio_disable_func(func);
|
|
|
|
sdio_release_host(func);
|
|
|
|
return ret;
|
2019-09-19 14:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wfx_sdio_remove(struct sdio_func *func)
|
|
|
|
{
|
2019-09-19 14:25:37 +00:00
|
|
|
struct wfx_sdio_priv *bus = sdio_get_drvdata(func);
|
|
|
|
|
2019-09-19 14:25:38 +00:00
|
|
|
wfx_release(bus->core);
|
2019-09-19 14:25:37 +00:00
|
|
|
sdio_claim_host(func);
|
|
|
|
sdio_disable_func(func);
|
|
|
|
sdio_release_host(func);
|
2019-09-19 14:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct sdio_device_id wfx_sdio_ids[] = {
|
2022-02-16 10:31:11 +01:00
|
|
|
/* WF200 does not have official VID/PID */
|
|
|
|
{ SDIO_DEVICE(0x0000, 0x1000) },
|
2019-09-19 14:25:36 +00:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(sdio, wfx_sdio_ids);
|
|
|
|
|
2025-03-04 16:32:23 +01:00
|
|
|
static DEFINE_SIMPLE_DEV_PM_OPS(wfx_sdio_pm_ops, wfx_sdio_suspend, wfx_sdio_resume);
|
|
|
|
|
2019-09-19 14:25:36 +00:00
|
|
|
struct sdio_driver wfx_sdio_driver = {
|
|
|
|
.name = "wfx-sdio",
|
|
|
|
.id_table = wfx_sdio_ids,
|
|
|
|
.probe = wfx_sdio_probe,
|
|
|
|
.remove = wfx_sdio_remove,
|
|
|
|
.drv = {
|
2020-04-29 16:21:09 +02:00
|
|
|
.of_match_table = wfx_sdio_of_match,
|
2025-03-04 16:32:23 +01:00
|
|
|
.pm = &wfx_sdio_pm_ops,
|
2019-09-19 14:25:36 +00:00
|
|
|
}
|
|
|
|
};
|