// SPDX-License-Identifier: GPL-2.0-only /* * DDR PHY Front End (DPFE) driver for Broadcom set top box SoCs * * Copyright (c) 2017 Broadcom
*/
/* * This driver provides access to the DPFE interface of Broadcom STB SoCs. * The firmware running on the DCPU inside the DDR PHY can provide current * information about the system's RAM, for instance the DRAM refresh rate. * This can be used as an indirect indicator for the DRAM's temperature. * Slower refresh rate means cooler RAM, higher refresh rate means hotter * RAM. * * Throughout the driver, we use readl_relaxed() and writel_relaxed(), which * already contain the appropriate le32_to_cpu()/cpu_to_le32() calls. * * Note regarding the loading of the firmware image: we use be32_to_cpu() * and le_32_to_cpu(), so we can support the following four cases: * - LE kernel + LE firmware image (the most common case) * - LE kernel + BE firmware image * - BE kernel + LE firmware image * - BE kernel + BE firmware image * * The DPCU always runs in big endian mode. The firmware image, however, can * be in either format. Also, communication between host CPU and DCPU is * always in little endian.
*/
/* * Format of the binary firmware file: * * entry * 0 header * value: 0xfe0101fe <== little endian * 0xfe1010fe <== big endian * 1 sequence: * [31:16] total segments on this build * [15:0] this segment sequence. * 2 FW version * 3 IMEM byte size * 4 DMEM byte size * IMEM * DMEM * last checksum ==> sum of everything
*/ struct dpfe_firmware_header {
u32 magic;
u32 sequence;
u32 version;
u32 imem_size;
u32 dmem_size;
};
/* Things we only need during initialization. */ struct init_data { unsignedint dmem_len; unsignedint imem_len; unsignedint chksum; bool is_big_endian;
};
/* API version and corresponding commands */ struct dpfe_api { int version; constchar *fw_name; conststruct attribute_group **sysfs_attrs;
u32 command[DPFE_CMD_MAX][MSG_FIELD_MAX];
};
/* Things we need for as long as we are active. */ struct brcmstb_dpfe_priv { void __iomem *regs; void __iomem *dmem; void __iomem *imem; struct device *dev; conststruct dpfe_api *dpfe_api; struct mutex lock;
};
/* * Declare our attributes early, so they can be referenced in the API data * structure. We need to do this, because the attributes depend on the API * version.
*/ static DEVICE_ATTR(dpfe_info, 0444, show_info, NULL); static DEVICE_ATTR(dpfe_refresh, 0644, show_refresh, store_refresh); static DEVICE_ATTR(dpfe_vendor, 0444, show_vendor, NULL); static DEVICE_ATTR(dpfe_dram, 0444, show_dram, NULL);
/* * Old API v2 firmware commands, as defined in the rev 0.61 specification, we * use a version set to 1 to denote that it is not compatible with the new API * v2 and onwards.
*/ staticconststruct dpfe_api dpfe_api_old_v2 = {
.version = 1,
.fw_name = "dpfe.bin",
.sysfs_attrs = dpfe_v2_groups,
.command = {
[DPFE_CMD_GET_INFO] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 1,
[MSG_ARG_COUNT] = 1,
[MSG_ARG0] = 1,
},
[DPFE_CMD_GET_REFRESH] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 2,
[MSG_ARG_COUNT] = 1,
[MSG_ARG0] = 1,
},
[DPFE_CMD_GET_VENDOR] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 2,
[MSG_ARG_COUNT] = 1,
[MSG_ARG0] = 2,
},
}
};
/* * API v2 firmware commands, as defined in the rev 0.8 specification, named new * v2 here
*/ staticconststruct dpfe_api dpfe_api_new_v2 = {
.version = 2,
.fw_name = NULL, /* We expect the firmware to have been downloaded! */
.sysfs_attrs = dpfe_v2_groups,
.command = {
[DPFE_CMD_GET_INFO] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 0x101,
},
[DPFE_CMD_GET_REFRESH] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 0x201,
},
[DPFE_CMD_GET_VENDOR] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 0x202,
},
}
};
/* API v3 firmware commands */ staticconststruct dpfe_api dpfe_api_v3 = {
.version = 3,
.fw_name = NULL, /* We expect the firmware to have been downloaded! */
.sysfs_attrs = dpfe_v3_groups,
.command = {
[DPFE_CMD_GET_INFO] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 0x0101,
[MSG_ARG_COUNT] = 1,
[MSG_ARG0] = 1,
},
[DPFE_CMD_GET_REFRESH] = {
[MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
[MSG_COMMAND] = 0x0202,
[MSG_ARG_COUNT] = 0,
}, /* There's no GET_VENDOR command in API v3. */
},
};
/* Put DCPU in reset if it's running. */
val = readl_relaxed(priv->regs + REG_DCPU_RESET);
val |= (1 << DCPU_RESET_SHIFT);
writel_relaxed(val, priv->regs + REG_DCPU_RESET);
/* * msg_type == 1: the offset is relative to the message RAM * msg_type == 0: the offset is relative to the data RAM (this is the * previous way of passing data) * msg_type is anything else: there's critical hardware problem
*/ switch (msg_type) { case 1:
ptr = priv->regs + DCPU_MSG_RAM_START + offset; break; case 0:
ptr = priv->dmem + offset; break; default:
dev_emerg(priv->dev, "invalid message reply from DCPU: %#x\n",
response); if (buf && size)
*size = sprintf(buf, "FATAL: communication error with DCPU\n");
}
/* * It depends on the API version which MBOX register we have to write to * signal we are done.
*/
release_mbox = (priv->dpfe_api->version < 2)
? REG_TO_HOST_MBOX : REG_TO_DCPU_MBOX;
writel_relaxed(0, priv->regs + release_mbox);
}
staticint __send_command(struct brcmstb_dpfe_priv *priv, unsignedint cmd,
u32 result[])
{ void __iomem *regs = priv->regs; unsignedint i, chksum, chksum_idx; const u32 *msg; int ret = 0;
u32 resp;
if (cmd >= DPFE_CMD_MAX) return -1;
msg = priv->dpfe_api->command[cmd];
mutex_lock(&priv->lock);
/* Wait for DCPU to become ready */ for (i = 0; i < DELAY_LOOP_MAX; i++) {
resp = readl_relaxed(regs + REG_TO_HOST_MBOX); if (resp == 0) break;
msleep(1);
} if (resp != 0) {
mutex_unlock(&priv->lock); return -ffs(DCPU_RET_ERR_TIMEDOUT);
}
/* Compute checksum over the message */
chksum_idx = msg[MSG_ARG_COUNT] + MSG_ARG_COUNT + 1;
chksum = get_msg_chksum(msg, chksum_idx);
/* Write command and arguments to message area */ for (i = 0; i < MSG_FIELD_MAX; i++) { if (i == chksum_idx)
writel_relaxed(chksum, regs + DCPU_MSG_RAM(i)); else
writel_relaxed(msg[i], regs + DCPU_MSG_RAM(i));
}
/* Tell DCPU there is a command waiting */
writel_relaxed(1, regs + REG_TO_DCPU_MBOX);
/* Wait for DCPU to process the command */ for (i = 0; i < DELAY_LOOP_MAX; i++) { /* Read response code */
resp = readl_relaxed(regs + REG_TO_HOST_MBOX); if (resp > 0) break;
msleep(1);
}
if (i == DELAY_LOOP_MAX) {
resp = (DCPU_RET_ERR_TIMEDOUT & ~DCPU_RET_ERROR_BIT);
ret = -ffs(resp);
} else { /* Read response data */ for (i = 0; i < MSG_FIELD_MAX; i++)
result[i] = readl_relaxed(regs + DCPU_MSG_RAM(i));
chksum_idx = result[MSG_ARG_COUNT] + MSG_ARG_COUNT + 1;
}
/* Tell DCPU we are done */
__finalize_command(priv);
/* Data and instruction sections are 32 bit words. */ if ((dmem_size % sizeof(u32)) != 0 || (imem_size % sizeof(u32)) != 0) return ERR_INVALID_SIZE;
/* * The header + the data section + the instruction section + the * checksum must be equal to the total firmware size.
*/
total_size = dmem_size + imem_size + sizeof(*header) + sizeof(*chksum_ptr); if (total_size != fw->size) return ERR_INVALID_SIZE;
/* The checksum comes at the very end. */
chksum_ptr = (void *)fw->data + sizeof(*header) + dmem_size + imem_size;
/* Convert size to 32-bit words. */
size /= sizeof(u32);
/* It is recommended to clear the firmware area first. */ for (i = 0; i < size; i++)
writel_relaxed(0, mem + i);
/* Now copy it. */ if (is_big_endian) { for (i = 0; i < size; i++)
writel_relaxed(be32_to_cpu(fw[i]), mem + i);
} else { for (i = 0; i < size; i++)
writel_relaxed(le32_to_cpu(fw[i]), mem + i);
}
/* * Skip downloading the firmware if the DCPU is already running and * responding to commands.
*/ if (is_dcpu_enabled(priv)) {
u32 response[MSG_FIELD_MAX];
ret = __send_command(priv, DPFE_CMD_GET_INFO, response); if (!ret) return 0;
}
/* * If the firmware filename is NULL it means the boot firmware has to * download the DCPU firmware for us. If that didn't work, we have to * bail, since downloading it ourselves wouldn't work either.
*/ if (!priv->dpfe_api->fw_name) return -ENODEV;
ret = firmware_request_nowarn(&fw, priv->dpfe_api->fw_name, dev); /* * Defer the firmware download if the firmware file couldn't be found. * The root file system may not be available yet.
*/ if (ret) return (ret == -ENOENT) ? -EPROBE_DEFER : ret;
ret = __verify_firmware(&init, fw); if (ret) {
ret = -EFAULT; goto release_fw;
}
/* At the beginning of the firmware blob is a header. */
header = (struct dpfe_firmware_header *)fw->data; /* Void pointer to the beginning of the actual firmware. */
fw_blob = fw->data + sizeof(*header); /* IMEM comes right after the header. */
imem = fw_blob; /* DMEM follows after IMEM. */
dmem = fw_blob + imem_size;
ret = __write_firmware(priv->dmem, dmem, dmem_size, is_big_endian); if (ret) goto release_fw;
ret = __write_firmware(priv->imem, imem, imem_size, is_big_endian); if (ret) goto release_fw;
ret = __verify_fw_checksum(&init, priv, header, init.chksum); if (ret) goto release_fw;
priv->dpfe_api = of_device_get_match_data(dev); if (unlikely(!priv->dpfe_api)) { /* * It should be impossible to end up here, but to be safe we * check anyway.
*/
dev_err(dev, "Couldn't determine API\n"); return -ENOENT;
}
ret = brcmstb_dpfe_download_firmware(priv); if (ret) return dev_err_probe(dev, ret, "Couldn't download firmware\n");
ret = sysfs_create_groups(&pdev->dev.kobj, priv->dpfe_api->sysfs_attrs); if (!ret)
dev_info(dev, "registered with API v%d.\n",
priv->dpfe_api->version);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.