2 * DDR PHY Front End (DPFE) driver for Broadcom set top box SoCs
4 * Copyright (c) 2017 Broadcom
6 * Released under the GPLv2 only.
7 * SPDX-License-Identifier: GPL-2.0
11 * This driver provides access to the DPFE interface of Broadcom STB SoCs.
12 * The firmware running on the DCPU inside the DDR PHY can provide current
13 * information about the system's RAM, for instance the DRAM refresh rate.
14 * This can be used as an indirect indicator for the DRAM's temperature.
15 * Slower refresh rate means cooler RAM, higher refresh rate means hotter
18 * Throughout the driver, we use readl_relaxed() and writel_relaxed(), which
19 * already contain the appropriate le32_to_cpu()/cpu_to_le32() calls.
21 * Note regarding the loading of the firmware image: we use be32_to_cpu()
22 * and le_32_to_cpu(), so we can support the following four cases:
23 * - LE kernel + LE firmware image (the most common case)
24 * - LE kernel + BE firmware image
25 * - BE kernel + LE firmware image
26 * - BE kernel + BE firmware image
28 * The DPCU always runs in big endian mode. The firwmare image, however, can
29 * be in either format. Also, communication between host CPU and DCPU is
30 * always in little endian.
33 #include <linux/delay.h>
34 #include <linux/firmware.h>
36 #include <linux/module.h>
37 #include <linux/of_address.h>
38 #include <linux/platform_device.h>
40 #define DRVNAME "brcmstb-dpfe"
41 #define FIRMWARE_NAME "dpfe.bin"
43 /* DCPU register offsets */
44 #define REG_DCPU_RESET 0x0
45 #define REG_TO_DCPU_MBOX 0x10
46 #define REG_TO_HOST_MBOX 0x14
49 #define DCPU_MSG_RAM(x) (0x100 + (x) * sizeof(u32))
51 /* DRAM Info Offsets & Masks */
52 #define DRAM_INFO_INTERVAL 0x0
53 #define DRAM_INFO_MR4 0x4
54 #define DRAM_INFO_ERROR 0x8
55 #define DRAM_INFO_MR4_MASK 0xff
57 /* DRAM MR4 Offsets & Masks */
58 #define DRAM_MR4_REFRESH 0x0 /* Refresh rate */
59 #define DRAM_MR4_SR_ABORT 0x3 /* Self Refresh Abort */
60 #define DRAM_MR4_PPRE 0x4 /* Post-package repair entry/exit */
61 #define DRAM_MR4_TH_OFFS 0x5 /* Thermal Offset; vendor specific */
62 #define DRAM_MR4_TUF 0x7 /* Temperature Update Flag */
64 #define DRAM_MR4_REFRESH_MASK 0x7
65 #define DRAM_MR4_SR_ABORT_MASK 0x1
66 #define DRAM_MR4_PPRE_MASK 0x1
67 #define DRAM_MR4_TH_OFFS_MASK 0x3
68 #define DRAM_MR4_TUF_MASK 0x1
70 /* DRAM Vendor Offsets & Masks */
71 #define DRAM_VENDOR_MR5 0x0
72 #define DRAM_VENDOR_MR6 0x4
73 #define DRAM_VENDOR_MR7 0x8
74 #define DRAM_VENDOR_MR8 0xc
75 #define DRAM_VENDOR_ERROR 0x10
76 #define DRAM_VENDOR_MASK 0xff
78 /* Reset register bits & masks */
79 #define DCPU_RESET_SHIFT 0x0
80 #define DCPU_RESET_MASK 0x1
81 #define DCPU_CLK_DISABLE_SHIFT 0x2
83 /* DCPU return codes */
84 #define DCPU_RET_ERROR_BIT BIT(31)
85 #define DCPU_RET_SUCCESS 0x1
86 #define DCPU_RET_ERR_HEADER (DCPU_RET_ERROR_BIT | BIT(0))
87 #define DCPU_RET_ERR_INVAL (DCPU_RET_ERROR_BIT | BIT(1))
88 #define DCPU_RET_ERR_CHKSUM (DCPU_RET_ERROR_BIT | BIT(2))
89 #define DCPU_RET_ERR_COMMAND (DCPU_RET_ERROR_BIT | BIT(3))
90 /* This error code is not firmware defined and only used in the driver. */
91 #define DCPU_RET_ERR_TIMEDOUT (DCPU_RET_ERROR_BIT | BIT(4))
94 #define DPFE_BE_MAGIC 0xfe1010fe
95 #define DPFE_LE_MAGIC 0xfe0101fe
98 #define ERR_INVALID_MAGIC -1
99 #define ERR_INVALID_SIZE -2
100 #define ERR_INVALID_CHKSUM -3
103 #define DPFE_MSG_TYPE_COMMAND 1
104 #define DPFE_MSG_TYPE_RESPONSE 2
106 #define DELAY_LOOP_MAX 200000
108 enum dpfe_msg_fields
{
114 MSG_FIELD_MAX
/* Last entry */
119 DPFE_CMD_GET_REFRESH
,
121 DPFE_CMD_MAX
/* Last entry */
129 u32 chksum
; /* This is the sum of all other entries. */
133 * Format of the binary firmware file:
137 * value: 0xfe0101fe <== little endian
138 * 0xfe1010fe <== big endian
140 * [31:16] total segments on this build
141 * [15:0] this segment sequence.
147 * last checksum ==> sum of everything
149 struct dpfe_firmware_header
{
157 /* Things we only need during initialization. */
159 unsigned int dmem_len
;
160 unsigned int imem_len
;
165 /* Things we need for as long as we are active. */
166 struct private_data
{
175 static const char *error_text
[] = {
176 "Success", "Header code incorrect", "Unknown command or argument",
177 "Incorrect checksum", "Malformed command", "Timed out",
180 /* List of supported firmware commands */
181 static const u32 dpfe_commands
[DPFE_CMD_MAX
][MSG_FIELD_MAX
] = {
182 [DPFE_CMD_GET_INFO
] = {
183 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
189 [DPFE_CMD_GET_REFRESH
] = {
190 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
196 [DPFE_CMD_GET_VENDOR
] = {
197 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
205 static bool is_dcpu_enabled(void __iomem
*regs
)
209 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
211 return !(val
& DCPU_RESET_MASK
);
214 static void __disable_dcpu(void __iomem
*regs
)
218 if (!is_dcpu_enabled(regs
))
221 /* Put DCPU in reset if it's running. */
222 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
223 val
|= (1 << DCPU_RESET_SHIFT
);
224 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
227 static void __enable_dcpu(void __iomem
*regs
)
231 /* Clear mailbox registers. */
232 writel_relaxed(0, regs
+ REG_TO_DCPU_MBOX
);
233 writel_relaxed(0, regs
+ REG_TO_HOST_MBOX
);
235 /* Disable DCPU clock gating */
236 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
237 val
&= ~(1 << DCPU_CLK_DISABLE_SHIFT
);
238 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
240 /* Take DCPU out of reset */
241 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
242 val
&= ~(1 << DCPU_RESET_SHIFT
);
243 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
246 static unsigned int get_msg_chksum(const u32 msg
[])
248 unsigned int sum
= 0;
251 /* Don't include the last field in the checksum. */
252 for (i
= 0; i
< MSG_FIELD_MAX
- 1; i
++)
258 static int __send_command(struct private_data
*priv
, unsigned int cmd
,
261 const u32
*msg
= dpfe_commands
[cmd
];
262 void __iomem
*regs
= priv
->regs
;
263 unsigned int i
, chksum
;
267 if (cmd
>= DPFE_CMD_MAX
)
270 mutex_lock(&priv
->lock
);
272 /* Write command and arguments to message area */
273 for (i
= 0; i
< MSG_FIELD_MAX
; i
++)
274 writel_relaxed(msg
[i
], regs
+ DCPU_MSG_RAM(i
));
276 /* Tell DCPU there is a command waiting */
277 writel_relaxed(1, regs
+ REG_TO_DCPU_MBOX
);
279 /* Wait for DCPU to process the command */
280 for (i
= 0; i
< DELAY_LOOP_MAX
; i
++) {
281 /* Read response code */
282 resp
= readl_relaxed(regs
+ REG_TO_HOST_MBOX
);
288 if (i
== DELAY_LOOP_MAX
) {
289 resp
= (DCPU_RET_ERR_TIMEDOUT
& ~DCPU_RET_ERROR_BIT
);
292 /* Read response data */
293 for (i
= 0; i
< MSG_FIELD_MAX
; i
++)
294 result
[i
] = readl_relaxed(regs
+ DCPU_MSG_RAM(i
));
297 /* Tell DCPU we are done */
298 writel_relaxed(0, regs
+ REG_TO_HOST_MBOX
);
300 mutex_unlock(&priv
->lock
);
305 /* Verify response */
306 chksum
= get_msg_chksum(result
);
307 if (chksum
!= result
[MSG_CHKSUM
])
308 resp
= DCPU_RET_ERR_CHKSUM
;
310 if (resp
!= DCPU_RET_SUCCESS
) {
311 resp
&= ~DCPU_RET_ERROR_BIT
;
318 /* Ensure that the firmware file loaded meets all the requirements. */
319 static int __verify_firmware(struct init_data
*init
,
320 const struct firmware
*fw
)
322 const struct dpfe_firmware_header
*header
= (void *)fw
->data
;
323 unsigned int dmem_size
, imem_size
, total_size
;
324 bool is_big_endian
= false;
325 const u32
*chksum_ptr
;
327 if (header
->magic
== DPFE_BE_MAGIC
)
328 is_big_endian
= true;
329 else if (header
->magic
!= DPFE_LE_MAGIC
)
330 return ERR_INVALID_MAGIC
;
333 dmem_size
= be32_to_cpu(header
->dmem_size
);
334 imem_size
= be32_to_cpu(header
->imem_size
);
336 dmem_size
= le32_to_cpu(header
->dmem_size
);
337 imem_size
= le32_to_cpu(header
->imem_size
);
340 /* Data and instruction sections are 32 bit words. */
341 if ((dmem_size
% sizeof(u32
)) != 0 || (imem_size
% sizeof(u32
)) != 0)
342 return ERR_INVALID_SIZE
;
345 * The header + the data section + the instruction section + the
346 * checksum must be equal to the total firmware size.
348 total_size
= dmem_size
+ imem_size
+ sizeof(*header
) +
350 if (total_size
!= fw
->size
)
351 return ERR_INVALID_SIZE
;
353 /* The checksum comes at the very end. */
354 chksum_ptr
= (void *)fw
->data
+ sizeof(*header
) + dmem_size
+ imem_size
;
356 init
->is_big_endian
= is_big_endian
;
357 init
->dmem_len
= dmem_size
;
358 init
->imem_len
= imem_size
;
359 init
->chksum
= (is_big_endian
)
360 ? be32_to_cpu(*chksum_ptr
) : le32_to_cpu(*chksum_ptr
);
365 /* Verify checksum by reading back the firmware from co-processor RAM. */
366 static int __verify_fw_checksum(struct init_data
*init
,
367 struct private_data
*priv
,
368 const struct dpfe_firmware_header
*header
,
371 u32 magic
, sequence
, version
, sum
;
372 u32 __iomem
*dmem
= priv
->dmem
;
373 u32 __iomem
*imem
= priv
->imem
;
376 if (init
->is_big_endian
) {
377 magic
= be32_to_cpu(header
->magic
);
378 sequence
= be32_to_cpu(header
->sequence
);
379 version
= be32_to_cpu(header
->version
);
381 magic
= le32_to_cpu(header
->magic
);
382 sequence
= le32_to_cpu(header
->sequence
);
383 version
= le32_to_cpu(header
->version
);
386 sum
= magic
+ sequence
+ version
+ init
->dmem_len
+ init
->imem_len
;
388 for (i
= 0; i
< init
->dmem_len
/ sizeof(u32
); i
++)
389 sum
+= readl_relaxed(dmem
+ i
);
391 for (i
= 0; i
< init
->imem_len
/ sizeof(u32
); i
++)
392 sum
+= readl_relaxed(imem
+ i
);
394 return (sum
== checksum
) ? 0 : -1;
397 static int __write_firmware(u32 __iomem
*mem
, const u32
*fw
,
398 unsigned int size
, bool is_big_endian
)
402 /* Convert size to 32-bit words. */
405 /* It is recommended to clear the firmware area first. */
406 for (i
= 0; i
< size
; i
++)
407 writel_relaxed(0, mem
+ i
);
411 for (i
= 0; i
< size
; i
++)
412 writel_relaxed(be32_to_cpu(fw
[i
]), mem
+ i
);
414 for (i
= 0; i
< size
; i
++)
415 writel_relaxed(le32_to_cpu(fw
[i
]), mem
+ i
);
421 static int brcmstb_dpfe_download_firmware(struct platform_device
*pdev
,
422 struct init_data
*init
)
424 const struct dpfe_firmware_header
*header
;
425 unsigned int dmem_size
, imem_size
;
426 struct device
*dev
= &pdev
->dev
;
427 bool is_big_endian
= false;
428 struct private_data
*priv
;
429 const struct firmware
*fw
;
430 const u32
*dmem
, *imem
;
434 priv
= platform_get_drvdata(pdev
);
437 * Skip downloading the firmware if the DCPU is already running and
438 * responding to commands.
440 if (is_dcpu_enabled(priv
->regs
)) {
441 u32 response
[MSG_FIELD_MAX
];
443 ret
= __send_command(priv
, DPFE_CMD_GET_INFO
, response
);
448 ret
= request_firmware(&fw
, FIRMWARE_NAME
, dev
);
449 /* request_firmware() prints its own error messages. */
453 ret
= __verify_firmware(init
, fw
);
457 __disable_dcpu(priv
->regs
);
459 is_big_endian
= init
->is_big_endian
;
460 dmem_size
= init
->dmem_len
;
461 imem_size
= init
->imem_len
;
463 /* At the beginning of the firmware blob is a header. */
464 header
= (struct dpfe_firmware_header
*)fw
->data
;
465 /* Void pointer to the beginning of the actual firmware. */
466 fw_blob
= fw
->data
+ sizeof(*header
);
467 /* IMEM comes right after the header. */
469 /* DMEM follows after IMEM. */
470 dmem
= fw_blob
+ imem_size
;
472 ret
= __write_firmware(priv
->dmem
, dmem
, dmem_size
, is_big_endian
);
475 ret
= __write_firmware(priv
->imem
, imem
, imem_size
, is_big_endian
);
479 ret
= __verify_fw_checksum(init
, priv
, header
, init
->chksum
);
483 __enable_dcpu(priv
->regs
);
488 static ssize_t
generic_show(unsigned int command
, u32 response
[],
489 struct device
*dev
, char *buf
)
491 struct private_data
*priv
;
494 priv
= dev_get_drvdata(dev
);
496 return sprintf(buf
, "ERROR: driver private data not set\n");
498 ret
= __send_command(priv
, command
, response
);
500 return sprintf(buf
, "ERROR: %s\n", error_text
[-ret
]);
505 static ssize_t
show_info(struct device
*dev
, struct device_attribute
*devattr
,
508 u32 response
[MSG_FIELD_MAX
];
512 ret
= generic_show(DPFE_CMD_GET_INFO
, response
, dev
, buf
);
516 info
= response
[MSG_ARG0
];
518 return sprintf(buf
, "%u.%u.%u.%u\n",
525 static ssize_t
show_refresh(struct device
*dev
,
526 struct device_attribute
*devattr
, char *buf
)
528 u32 response
[MSG_FIELD_MAX
];
530 struct private_data
*priv
;
532 u8 refresh
, sr_abort
, ppre
, thermal_offs
, tuf
;
536 ret
= generic_show(DPFE_CMD_GET_REFRESH
, response
, dev
, buf
);
540 priv
= dev_get_drvdata(dev
);
541 offset
= response
[MSG_ARG0
];
542 info
= priv
->dmem
+ offset
;
544 mr4
= readl_relaxed(info
+ DRAM_INFO_MR4
) & DRAM_INFO_MR4_MASK
;
546 refresh
= (mr4
>> DRAM_MR4_REFRESH
) & DRAM_MR4_REFRESH_MASK
;
547 sr_abort
= (mr4
>> DRAM_MR4_SR_ABORT
) & DRAM_MR4_SR_ABORT_MASK
;
548 ppre
= (mr4
>> DRAM_MR4_PPRE
) & DRAM_MR4_PPRE_MASK
;
549 thermal_offs
= (mr4
>> DRAM_MR4_TH_OFFS
) & DRAM_MR4_TH_OFFS_MASK
;
550 tuf
= (mr4
>> DRAM_MR4_TUF
) & DRAM_MR4_TUF_MASK
;
552 return sprintf(buf
, "%#x %#x %#x %#x %#x %#x %#x\n",
553 readl_relaxed(info
+ DRAM_INFO_INTERVAL
),
554 refresh
, sr_abort
, ppre
, thermal_offs
, tuf
,
555 readl_relaxed(info
+ DRAM_INFO_ERROR
));
558 static ssize_t
store_refresh(struct device
*dev
, struct device_attribute
*attr
,
559 const char *buf
, size_t count
)
561 u32 response
[MSG_FIELD_MAX
];
562 struct private_data
*priv
;
568 if (kstrtoul(buf
, 0, &val
) < 0)
571 priv
= dev_get_drvdata(dev
);
573 ret
= __send_command(priv
, DPFE_CMD_GET_REFRESH
, response
);
577 offset
= response
[MSG_ARG0
];
578 info
= priv
->dmem
+ offset
;
579 writel_relaxed(val
, info
+ DRAM_INFO_INTERVAL
);
584 static ssize_t
show_vendor(struct device
*dev
, struct device_attribute
*devattr
,
587 u32 response
[MSG_FIELD_MAX
];
588 struct private_data
*priv
;
593 ret
= generic_show(DPFE_CMD_GET_VENDOR
, response
, dev
, buf
);
597 offset
= response
[MSG_ARG0
];
598 priv
= dev_get_drvdata(dev
);
599 info
= priv
->dmem
+ offset
;
601 return sprintf(buf
, "%#x %#x %#x %#x %#x\n",
602 readl_relaxed(info
+ DRAM_VENDOR_MR5
) & DRAM_VENDOR_MASK
,
603 readl_relaxed(info
+ DRAM_VENDOR_MR6
) & DRAM_VENDOR_MASK
,
604 readl_relaxed(info
+ DRAM_VENDOR_MR7
) & DRAM_VENDOR_MASK
,
605 readl_relaxed(info
+ DRAM_VENDOR_MR8
) & DRAM_VENDOR_MASK
,
606 readl_relaxed(info
+ DRAM_VENDOR_ERROR
));
609 static int brcmstb_dpfe_resume(struct platform_device
*pdev
)
611 struct init_data init
;
613 return brcmstb_dpfe_download_firmware(pdev
, &init
);
616 static DEVICE_ATTR(dpfe_info
, 0444, show_info
, NULL
);
617 static DEVICE_ATTR(dpfe_refresh
, 0644, show_refresh
, store_refresh
);
618 static DEVICE_ATTR(dpfe_vendor
, 0444, show_vendor
, NULL
);
619 static struct attribute
*dpfe_attrs
[] = {
620 &dev_attr_dpfe_info
.attr
,
621 &dev_attr_dpfe_refresh
.attr
,
622 &dev_attr_dpfe_vendor
.attr
,
625 ATTRIBUTE_GROUPS(dpfe
);
627 static int brcmstb_dpfe_probe(struct platform_device
*pdev
)
629 struct device
*dev
= &pdev
->dev
;
630 struct private_data
*priv
;
631 struct device
*dpfe_dev
;
632 struct init_data init
;
633 struct resource
*res
;
637 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
641 mutex_init(&priv
->lock
);
642 platform_set_drvdata(pdev
, priv
);
644 /* Cell index is optional; default to 0 if not present. */
645 ret
= of_property_read_u32(dev
->of_node
, "cell-index", &index
);
649 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-cpu");
650 priv
->regs
= devm_ioremap_resource(dev
, res
);
651 if (IS_ERR(priv
->regs
)) {
652 dev_err(dev
, "couldn't map DCPU registers\n");
656 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-dmem");
657 priv
->dmem
= devm_ioremap_resource(dev
, res
);
658 if (IS_ERR(priv
->dmem
)) {
659 dev_err(dev
, "Couldn't map DCPU data memory\n");
663 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-imem");
664 priv
->imem
= devm_ioremap_resource(dev
, res
);
665 if (IS_ERR(priv
->imem
)) {
666 dev_err(dev
, "Couldn't map DCPU instruction memory\n");
670 ret
= brcmstb_dpfe_download_firmware(pdev
, &init
);
674 dpfe_dev
= devm_kzalloc(dev
, sizeof(*dpfe_dev
), GFP_KERNEL
);
680 priv
->dev
= dpfe_dev
;
683 dpfe_dev
->parent
= dev
;
684 dpfe_dev
->groups
= dpfe_groups
;
685 dpfe_dev
->of_node
= dev
->of_node
;
686 dev_set_drvdata(dpfe_dev
, priv
);
687 dev_set_name(dpfe_dev
, "dpfe%u", index
);
689 ret
= device_register(dpfe_dev
);
693 dev_info(dev
, "registered.\n");
698 dev_err(dev
, "failed to initialize -- error %d\n", ret
);
703 static const struct of_device_id brcmstb_dpfe_of_match
[] = {
704 { .compatible
= "brcm,dpfe-cpu", },
707 MODULE_DEVICE_TABLE(of
, brcmstb_dpfe_of_match
);
709 static struct platform_driver brcmstb_dpfe_driver
= {
712 .of_match_table
= brcmstb_dpfe_of_match
,
714 .probe
= brcmstb_dpfe_probe
,
715 .resume
= brcmstb_dpfe_resume
,
718 module_platform_driver(brcmstb_dpfe_driver
);
720 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
721 MODULE_DESCRIPTION("BRCMSTB DDR PHY Front End Driver");
722 MODULE_LICENSE("GPL");