blob: fe8c3b1fa9a34cf0fe2f12d957789dece4980e06 [file] [log] [blame]
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <mach/msm_iomap.h>
#include <mach/subsystem_restart.h>
#include <mach/msm_smsm.h>
#include <mach/peripheral-loader.h>
#include "smd_private.h"
#include "ramdump.h"
#include "peripheral-loader.h"
#include "pil-q6v4.h"
#include "scm-pas.h"
#define MSS_S_HCLK_CTL (MSM_CLK_CTL_BASE + 0x2C70)
#define MSS_SLP_CLK_CTL (MSM_CLK_CTL_BASE + 0x2C60)
#define SFAB_MSS_M_ACLK_CTL (MSM_CLK_CTL_BASE + 0x2340)
#define SFAB_MSS_S_HCLK_CTL (MSM_CLK_CTL_BASE + 0x2C00)
#define MSS_RESET (MSM_CLK_CTL_BASE + 0x2C64)
struct q6v4_modem {
struct q6v4_data q6_fw;
struct q6v4_data q6_sw;
void __iomem *modem_base;
void *fw_ramdump_dev;
void *sw_ramdump_dev;
void *smem_ramdump_dev;
struct subsys_device *subsys;
struct subsys_desc subsys_desc;
int crash_shutdown;
int loadable;
void *pil;
};
static DEFINE_MUTEX(pil_q6v4_modem_lock);
static unsigned pil_q6v4_modem_count;
/* Bring modem subsystem out of reset */
static void pil_q6v4_init_modem(void __iomem *base, void __iomem *jtag_clk)
{
mutex_lock(&pil_q6v4_modem_lock);
if (!pil_q6v4_modem_count) {
/* Enable MSS clocks */
writel_relaxed(0x10, SFAB_MSS_M_ACLK_CTL);
writel_relaxed(0x10, SFAB_MSS_S_HCLK_CTL);
writel_relaxed(0x10, MSS_S_HCLK_CTL);
writel_relaxed(0x10, MSS_SLP_CLK_CTL);
/* Wait for clocks to enable */
mb();
udelay(10);
/* De-assert MSS reset */
writel_relaxed(0x0, MSS_RESET);
mb();
udelay(10);
/* Enable MSS */
writel_relaxed(0x7, base);
}
/* Enable JTAG clocks */
/* TODO: Remove if/when Q6 software enables them? */
writel_relaxed(0x10, jtag_clk);
pil_q6v4_modem_count++;
mutex_unlock(&pil_q6v4_modem_lock);
}
/* Put modem subsystem back into reset */
static void pil_q6v4_shutdown_modem(void)
{
mutex_lock(&pil_q6v4_modem_lock);
if (pil_q6v4_modem_count)
pil_q6v4_modem_count--;
if (pil_q6v4_modem_count == 0)
writel_relaxed(0x1, MSS_RESET);
mutex_unlock(&pil_q6v4_modem_lock);
}
static int pil_q6v4_modem_boot(struct pil_desc *pil)
{
struct q6v4_data *drv = pil_to_q6v4_data(pil);
struct q6v4_modem *mdm = dev_get_drvdata(pil->dev);
int err;
err = pil_q6v4_power_up(drv);
if (err)
return err;
pil_q6v4_init_modem(mdm->modem_base, drv->jtag_clk_reg);
return pil_q6v4_boot(pil);
}
static int pil_q6v4_modem_shutdown(struct pil_desc *pil)
{
struct q6v4_data *drv = pil_to_q6v4_data(pil);
int ret;
ret = pil_q6v4_shutdown(pil);
if (ret)
return ret;
pil_q6v4_shutdown_modem();
pil_q6v4_power_down(drv);
return 0;
}
static struct pil_reset_ops pil_q6v4_modem_ops = {
.init_image = pil_q6v4_init_image,
.auth_and_reset = pil_q6v4_modem_boot,
.shutdown = pil_q6v4_modem_shutdown,
.proxy_vote = pil_q6v4_make_proxy_votes,
.proxy_unvote = pil_q6v4_remove_proxy_votes,
};
static struct pil_reset_ops pil_q6v4_modem_ops_trusted = {
.init_image = pil_q6v4_init_image_trusted,
.auth_and_reset = pil_q6v4_boot_trusted,
.shutdown = pil_q6v4_shutdown_trusted,
.proxy_vote = pil_q6v4_make_proxy_votes,
.proxy_unvote = pil_q6v4_remove_proxy_votes,
};
static void log_modem_sfr(void)
{
u32 size;
char *smem_reason, reason[81];
smem_reason = smem_get_entry(SMEM_SSR_REASON_MSS0, &size);
if (!smem_reason || !size) {
pr_err("modem subsystem failure reason: (unknown, smem_get_entry failed).\n");
return;
}
if (!smem_reason[0]) {
pr_err("modem subsystem failure reason: (unknown, init string found).\n");
return;
}
size = min(size, sizeof(reason)-1);
memcpy(reason, smem_reason, size);
reason[size] = '\0';
pr_err("modem subsystem failure reason: %s.\n", reason);
smem_reason[0] = '\0';
wmb();
}
static void restart_modem(struct q6v4_modem *drv)
{
log_modem_sfr();
subsystem_restart_dev(drv->subsys);
}
#define desc_to_modem(d) container_of(d, struct q6v4_modem, subsys_desc)
static int modem_start(const struct subsys_desc *desc)
{
struct q6v4_modem *drv = desc_to_modem(desc);
if (drv->loadable) {
drv->pil = pil_get("modem");
if (IS_ERR(drv->pil))
return PTR_ERR(drv->pil);
}
return 0;
}
static void modem_stop(const struct subsys_desc *desc)
{
struct q6v4_modem *drv = desc_to_modem(desc);
if (drv->loadable)
pil_put(drv->pil);
}
static int modem_shutdown(const struct subsys_desc *subsys)
{
struct q6v4_modem *drv = desc_to_modem(subsys);
/* The watchdogs keep running even after the modem is shutdown */
writel_relaxed(0x0, drv->q6_fw.wdog_base + 0x24);
writel_relaxed(0x0, drv->q6_sw.wdog_base + 0x24);
mb();
if (drv->loadable) {
pil_force_shutdown("modem");
pil_force_shutdown("modem_fw");
}
disable_irq_nosync(drv->q6_fw.wdog_irq);
disable_irq_nosync(drv->q6_sw.wdog_irq);
return 0;
}
static int modem_powerup(const struct subsys_desc *subsys)
{
struct q6v4_modem *drv = desc_to_modem(subsys);
if (drv->loadable) {
pil_force_boot("modem_fw");
pil_force_boot("modem");
}
enable_irq(drv->q6_fw.wdog_irq);
enable_irq(drv->q6_sw.wdog_irq);
return 0;
}
void modem_crash_shutdown(const struct subsys_desc *subsys)
{
struct q6v4_modem *drv = desc_to_modem(subsys);
drv->crash_shutdown = 1;
smsm_reset_modem(SMSM_RESET);
}
static struct ramdump_segment sw_segments[] = {
{0x89000000, 0x8D400000 - 0x89000000},
};
static struct ramdump_segment fw_segments[] = {
{0x8D400000, 0x8DA00000 - 0x8D400000},
};
static struct ramdump_segment smem_segments[] = {
{0x80000000, 0x00200000},
};
static int modem_ramdump(int enable, const struct subsys_desc *subsys)
{
struct q6v4_modem *drv = desc_to_modem(subsys);
int ret;
if (!enable)
return 0;
ret = do_ramdump(drv->sw_ramdump_dev, sw_segments,
ARRAY_SIZE(sw_segments));
if (ret < 0)
return ret;
ret = do_ramdump(drv->fw_ramdump_dev, fw_segments,
ARRAY_SIZE(fw_segments));
if (ret < 0)
return ret;
ret = do_ramdump(drv->smem_ramdump_dev, smem_segments,
ARRAY_SIZE(smem_segments));
if (ret < 0)
return ret;
return 0;
}
static void smsm_state_cb(void *data, uint32_t old_state, uint32_t new_state)
{
struct q6v4_modem *drv = data;
/* Ignore if we're the one that set SMSM_RESET */
if (drv->crash_shutdown)
return;
if (new_state & SMSM_RESET) {
pr_err("Probable fatal error on the modem.\n");
restart_modem(drv);
}
}
static irqreturn_t modem_wdog_bite_irq(int irq, void *dev_id)
{
struct q6v4_modem *drv = dev_id;
restart_modem(drv);
return IRQ_HANDLED;
}
static int __devinit
pil_q6v4_proc_init(struct q6v4_data *drv, struct platform_device *pdev, int i)
{
static const char *name[2] = { "fw", "sw" };
const struct pil_q6v4_pdata *pdata_p = pdev->dev.platform_data;
const struct pil_q6v4_pdata *pdata = pdata_p + i;
char reg_name[12];
struct pil_desc *desc;
struct resource *res;
res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + (i * 2));
if (!res)
return -EINVAL;
drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
if (!drv->base)
return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + (i * 2));
if (!res)
return -EINVAL;
drv->wdog_base = devm_ioremap(&pdev->dev, res->start,
resource_size(res));
if (!drv->wdog_base)
return -ENOMEM;
snprintf(reg_name, sizeof(reg_name), "%s_core_vdd", name[i]);
drv->vreg = devm_regulator_get(&pdev->dev, reg_name);
if (IS_ERR(drv->vreg))
return PTR_ERR(drv->vreg);
drv->xo = devm_clk_get(&pdev->dev, "xo");
if (IS_ERR(drv->xo))
return PTR_ERR(drv->xo);
desc = &drv->desc;
desc->name = pdata->name;
desc->depends_on = pdata->depends;
desc->dev = &pdev->dev;
desc->owner = THIS_MODULE;
desc->proxy_timeout = 10000;
pil_q6v4_init(drv, pdata);
if (pas_supported(pdata->pas_id) > 0) {
desc->ops = &pil_q6v4_modem_ops_trusted;
dev_info(&pdev->dev, "using secure boot for %s\n", name[i]);
} else {
desc->ops = &pil_q6v4_modem_ops;
dev_info(&pdev->dev, "using non-secure boot for %s\n", name[i]);
}
return 0;
}
static int __devinit pil_q6v4_modem_driver_probe(struct platform_device *pdev)
{
struct q6v4_data *drv_fw, *drv_sw;
struct q6v4_modem *drv;
struct resource *res;
struct regulator *pll_supply;
int ret;
const struct pil_q6v4_pdata *pdata = pdev->dev.platform_data;
drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
if (!drv)
return -ENOMEM;
platform_set_drvdata(pdev, drv);
drv_fw = &drv->q6_fw;
drv_sw = &drv->q6_sw;
drv_fw->wdog_irq = platform_get_irq(pdev, 0);
if (drv_fw->wdog_irq < 0)
return drv_fw->wdog_irq;
drv_sw->wdog_irq = platform_get_irq(pdev, 1);
if (drv_sw->wdog_irq < 0)
return drv_sw->wdog_irq;
drv->loadable = !!pdata; /* No pdata = don't use PIL */
if (drv->loadable) {
ret = pil_q6v4_proc_init(drv_fw, pdev, 0);
if (ret)
return ret;
ret = pil_q6v4_proc_init(drv_sw, pdev, 1);
if (ret)
return ret;
pll_supply = devm_regulator_get(&pdev->dev, "pll_vdd");
drv_fw->pll_supply = drv_sw->pll_supply = pll_supply;
if (IS_ERR(pll_supply))
return PTR_ERR(pll_supply);
ret = regulator_set_voltage(pll_supply, 1800000, 1800000);
if (ret) {
dev_err(&pdev->dev, "failed to set pll voltage\n");
return ret;
}
ret = regulator_set_optimum_mode(pll_supply, 100000);
if (ret < 0) {
dev_err(&pdev->dev, "failed to set pll optimum mode\n");
return ret;
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
return -EINVAL;
drv->modem_base = devm_ioremap(&pdev->dev, res->start,
resource_size(res));
if (!drv->modem_base)
return -ENOMEM;
drv_fw->pil = msm_pil_register(&drv_fw->desc);
if (IS_ERR(drv_fw->pil))
return PTR_ERR(drv_fw->pil);
drv_sw->pil = msm_pil_register(&drv_sw->desc);
if (IS_ERR(drv_sw->pil)) {
ret = PTR_ERR(drv_sw->pil);
goto err_pil_sw;
}
}
drv->subsys_desc.name = "modem";
drv->subsys_desc.depends_on = "lpass";
drv->subsys_desc.dev = &pdev->dev;
drv->subsys_desc.owner = THIS_MODULE;
drv->subsys_desc.start = modem_start;
drv->subsys_desc.stop = modem_stop;
drv->subsys_desc.shutdown = modem_shutdown;
drv->subsys_desc.powerup = modem_powerup;
drv->subsys_desc.ramdump = modem_ramdump;
drv->subsys_desc.crash_shutdown = modem_crash_shutdown;
drv->fw_ramdump_dev = create_ramdump_device("modem_fw");
if (!drv->fw_ramdump_dev) {
ret = -ENOMEM;
goto err_fw_ramdump;
}
drv->sw_ramdump_dev = create_ramdump_device("modem_sw");
if (!drv->sw_ramdump_dev) {
ret = -ENOMEM;
goto err_sw_ramdump;
}
drv->smem_ramdump_dev = create_ramdump_device("smem-modem");
if (!drv->smem_ramdump_dev) {
ret = -ENOMEM;
goto err_smem_ramdump;
}
drv->subsys = subsys_register(&drv->subsys_desc);
if (IS_ERR(drv->subsys)) {
ret = PTR_ERR(drv->subsys);
goto err_subsys;
}
ret = devm_request_irq(&pdev->dev, drv_fw->wdog_irq,
modem_wdog_bite_irq, IRQF_TRIGGER_RISING,
dev_name(&pdev->dev), drv);
if (ret)
goto err_irq;
ret = devm_request_irq(&pdev->dev, drv_sw->wdog_irq,
modem_wdog_bite_irq, IRQF_TRIGGER_RISING,
dev_name(&pdev->dev), drv);
if (ret)
goto err_irq;
ret = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_RESET,
smsm_state_cb, drv);
if (ret)
goto err_irq;
return 0;
err_irq:
subsys_unregister(drv->subsys);
err_subsys:
destroy_ramdump_device(drv->smem_ramdump_dev);
err_smem_ramdump:
destroy_ramdump_device(drv->sw_ramdump_dev);
err_sw_ramdump:
destroy_ramdump_device(drv->fw_ramdump_dev);
err_fw_ramdump:
if (drv->loadable)
msm_pil_unregister(drv_sw->pil);
err_pil_sw:
msm_pil_unregister(drv_fw->pil);
return ret;
}
static int __devexit pil_q6v4_modem_driver_exit(struct platform_device *pdev)
{
struct q6v4_modem *drv = platform_get_drvdata(pdev);
smsm_state_cb_deregister(SMSM_MODEM_STATE, SMSM_RESET,
smsm_state_cb, drv);
subsys_unregister(drv->subsys);
destroy_ramdump_device(drv->smem_ramdump_dev);
destroy_ramdump_device(drv->sw_ramdump_dev);
destroy_ramdump_device(drv->fw_ramdump_dev);
if (drv->loadable) {
msm_pil_unregister(drv->q6_sw.pil);
msm_pil_unregister(drv->q6_fw.pil);
}
return 0;
}
static struct platform_driver pil_q6v4_modem_driver = {
.probe = pil_q6v4_modem_driver_probe,
.remove = __devexit_p(pil_q6v4_modem_driver_exit),
.driver = {
.name = "pil-q6v4-modem",
.owner = THIS_MODULE,
},
};
static int __init pil_q6v4_modem_init(void)
{
return platform_driver_register(&pil_q6v4_modem_driver);
}
module_init(pil_q6v4_modem_init);
static void __exit pil_q6v4_modem_exit(void)
{
platform_driver_unregister(&pil_q6v4_modem_driver);
}
module_exit(pil_q6v4_modem_exit);
MODULE_DESCRIPTION("Support for booting QDSP6v4 (Hexagon) processors");
MODULE_LICENSE("GPL v2");