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
48 /* Macros to process offsets returned by the DCPU */
49 #define DRAM_MSG_ADDR_OFFSET 0x0
50 #define DRAM_MSG_TYPE_OFFSET 0x1c
51 #define DRAM_MSG_ADDR_MASK ((1UL << DRAM_MSG_TYPE_OFFSET) - 1)
52 #define DRAM_MSG_TYPE_MASK ((1UL << \
53 (BITS_PER_LONG - DRAM_MSG_TYPE_OFFSET)) - 1)
56 #define DCPU_MSG_RAM_START 0x100
57 #define DCPU_MSG_RAM(x) (DCPU_MSG_RAM_START + (x) * sizeof(u32))
59 /* DRAM Info Offsets & Masks */
60 #define DRAM_INFO_INTERVAL 0x0
61 #define DRAM_INFO_MR4 0x4
62 #define DRAM_INFO_ERROR 0x8
63 #define DRAM_INFO_MR4_MASK 0xff
65 /* DRAM MR4 Offsets & Masks */
66 #define DRAM_MR4_REFRESH 0x0 /* Refresh rate */
67 #define DRAM_MR4_SR_ABORT 0x3 /* Self Refresh Abort */
68 #define DRAM_MR4_PPRE 0x4 /* Post-package repair entry/exit */
69 #define DRAM_MR4_TH_OFFS 0x5 /* Thermal Offset; vendor specific */
70 #define DRAM_MR4_TUF 0x7 /* Temperature Update Flag */
72 #define DRAM_MR4_REFRESH_MASK 0x7
73 #define DRAM_MR4_SR_ABORT_MASK 0x1
74 #define DRAM_MR4_PPRE_MASK 0x1
75 #define DRAM_MR4_TH_OFFS_MASK 0x3
76 #define DRAM_MR4_TUF_MASK 0x1
78 /* DRAM Vendor Offsets & Masks */
79 #define DRAM_VENDOR_MR5 0x0
80 #define DRAM_VENDOR_MR6 0x4
81 #define DRAM_VENDOR_MR7 0x8
82 #define DRAM_VENDOR_MR8 0xc
83 #define DRAM_VENDOR_ERROR 0x10
84 #define DRAM_VENDOR_MASK 0xff
86 /* Reset register bits & masks */
87 #define DCPU_RESET_SHIFT 0x0
88 #define DCPU_RESET_MASK 0x1
89 #define DCPU_CLK_DISABLE_SHIFT 0x2
91 /* DCPU return codes */
92 #define DCPU_RET_ERROR_BIT BIT(31)
93 #define DCPU_RET_SUCCESS 0x1
94 #define DCPU_RET_ERR_HEADER (DCPU_RET_ERROR_BIT | BIT(0))
95 #define DCPU_RET_ERR_INVAL (DCPU_RET_ERROR_BIT | BIT(1))
96 #define DCPU_RET_ERR_CHKSUM (DCPU_RET_ERROR_BIT | BIT(2))
97 #define DCPU_RET_ERR_COMMAND (DCPU_RET_ERROR_BIT | BIT(3))
98 /* This error code is not firmware defined and only used in the driver. */
99 #define DCPU_RET_ERR_TIMEDOUT (DCPU_RET_ERROR_BIT | BIT(4))
102 #define DPFE_BE_MAGIC 0xfe1010fe
103 #define DPFE_LE_MAGIC 0xfe0101fe
106 #define ERR_INVALID_MAGIC -1
107 #define ERR_INVALID_SIZE -2
108 #define ERR_INVALID_CHKSUM -3
111 #define DPFE_MSG_TYPE_COMMAND 1
112 #define DPFE_MSG_TYPE_RESPONSE 2
114 #define DELAY_LOOP_MAX 200000
116 enum dpfe_msg_fields
{
122 MSG_FIELD_MAX
/* Last entry */
127 DPFE_CMD_GET_REFRESH
,
129 DPFE_CMD_MAX
/* Last entry */
137 u32 chksum
; /* This is the sum of all other entries. */
141 * Format of the binary firmware file:
145 * value: 0xfe0101fe <== little endian
146 * 0xfe1010fe <== big endian
148 * [31:16] total segments on this build
149 * [15:0] this segment sequence.
155 * last checksum ==> sum of everything
157 struct dpfe_firmware_header
{
165 /* Things we only need during initialization. */
167 unsigned int dmem_len
;
168 unsigned int imem_len
;
173 /* Things we need for as long as we are active. */
174 struct private_data
{
182 static const char *error_text
[] = {
183 "Success", "Header code incorrect", "Unknown command or argument",
184 "Incorrect checksum", "Malformed command", "Timed out",
187 /* List of supported firmware commands */
188 static const u32 dpfe_commands
[DPFE_CMD_MAX
][MSG_FIELD_MAX
] = {
189 [DPFE_CMD_GET_INFO
] = {
190 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
196 [DPFE_CMD_GET_REFRESH
] = {
197 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
203 [DPFE_CMD_GET_VENDOR
] = {
204 [MSG_HEADER
] = DPFE_MSG_TYPE_COMMAND
,
212 static bool is_dcpu_enabled(void __iomem
*regs
)
216 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
218 return !(val
& DCPU_RESET_MASK
);
221 static void __disable_dcpu(void __iomem
*regs
)
225 if (!is_dcpu_enabled(regs
))
228 /* Put DCPU in reset if it's running. */
229 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
230 val
|= (1 << DCPU_RESET_SHIFT
);
231 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
234 static void __enable_dcpu(void __iomem
*regs
)
238 /* Clear mailbox registers. */
239 writel_relaxed(0, regs
+ REG_TO_DCPU_MBOX
);
240 writel_relaxed(0, regs
+ REG_TO_HOST_MBOX
);
242 /* Disable DCPU clock gating */
243 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
244 val
&= ~(1 << DCPU_CLK_DISABLE_SHIFT
);
245 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
247 /* Take DCPU out of reset */
248 val
= readl_relaxed(regs
+ REG_DCPU_RESET
);
249 val
&= ~(1 << DCPU_RESET_SHIFT
);
250 writel_relaxed(val
, regs
+ REG_DCPU_RESET
);
253 static unsigned int get_msg_chksum(const u32 msg
[])
255 unsigned int sum
= 0;
258 /* Don't include the last field in the checksum. */
259 for (i
= 0; i
< MSG_FIELD_MAX
- 1; i
++)
265 static void __iomem
*get_msg_ptr(struct private_data
*priv
, u32 response
,
266 char *buf
, ssize_t
*size
)
268 unsigned int msg_type
;
270 void __iomem
*ptr
= NULL
;
272 msg_type
= (response
>> DRAM_MSG_TYPE_OFFSET
) & DRAM_MSG_TYPE_MASK
;
273 offset
= (response
>> DRAM_MSG_ADDR_OFFSET
) & DRAM_MSG_ADDR_MASK
;
276 * msg_type == 1: the offset is relative to the message RAM
277 * msg_type == 0: the offset is relative to the data RAM (this is the
278 * previous way of passing data)
279 * msg_type is anything else: there's critical hardware problem
283 ptr
= priv
->regs
+ DCPU_MSG_RAM_START
+ offset
;
286 ptr
= priv
->dmem
+ offset
;
289 dev_emerg(priv
->dev
, "invalid message reply from DCPU: %#x\n",
293 "FATAL: communication error with DCPU\n");
299 static int __send_command(struct private_data
*priv
, unsigned int cmd
,
302 const u32
*msg
= dpfe_commands
[cmd
];
303 void __iomem
*regs
= priv
->regs
;
304 unsigned int i
, chksum
;
308 if (cmd
>= DPFE_CMD_MAX
)
311 mutex_lock(&priv
->lock
);
313 /* Write command and arguments to message area */
314 for (i
= 0; i
< MSG_FIELD_MAX
; i
++)
315 writel_relaxed(msg
[i
], regs
+ DCPU_MSG_RAM(i
));
317 /* Tell DCPU there is a command waiting */
318 writel_relaxed(1, regs
+ REG_TO_DCPU_MBOX
);
320 /* Wait for DCPU to process the command */
321 for (i
= 0; i
< DELAY_LOOP_MAX
; i
++) {
322 /* Read response code */
323 resp
= readl_relaxed(regs
+ REG_TO_HOST_MBOX
);
329 if (i
== DELAY_LOOP_MAX
) {
330 resp
= (DCPU_RET_ERR_TIMEDOUT
& ~DCPU_RET_ERROR_BIT
);
333 /* Read response data */
334 for (i
= 0; i
< MSG_FIELD_MAX
; i
++)
335 result
[i
] = readl_relaxed(regs
+ DCPU_MSG_RAM(i
));
338 /* Tell DCPU we are done */
339 writel_relaxed(0, regs
+ REG_TO_HOST_MBOX
);
341 mutex_unlock(&priv
->lock
);
346 /* Verify response */
347 chksum
= get_msg_chksum(result
);
348 if (chksum
!= result
[MSG_CHKSUM
])
349 resp
= DCPU_RET_ERR_CHKSUM
;
351 if (resp
!= DCPU_RET_SUCCESS
) {
352 resp
&= ~DCPU_RET_ERROR_BIT
;
359 /* Ensure that the firmware file loaded meets all the requirements. */
360 static int __verify_firmware(struct init_data
*init
,
361 const struct firmware
*fw
)
363 const struct dpfe_firmware_header
*header
= (void *)fw
->data
;
364 unsigned int dmem_size
, imem_size
, total_size
;
365 bool is_big_endian
= false;
366 const u32
*chksum_ptr
;
368 if (header
->magic
== DPFE_BE_MAGIC
)
369 is_big_endian
= true;
370 else if (header
->magic
!= DPFE_LE_MAGIC
)
371 return ERR_INVALID_MAGIC
;
374 dmem_size
= be32_to_cpu(header
->dmem_size
);
375 imem_size
= be32_to_cpu(header
->imem_size
);
377 dmem_size
= le32_to_cpu(header
->dmem_size
);
378 imem_size
= le32_to_cpu(header
->imem_size
);
381 /* Data and instruction sections are 32 bit words. */
382 if ((dmem_size
% sizeof(u32
)) != 0 || (imem_size
% sizeof(u32
)) != 0)
383 return ERR_INVALID_SIZE
;
386 * The header + the data section + the instruction section + the
387 * checksum must be equal to the total firmware size.
389 total_size
= dmem_size
+ imem_size
+ sizeof(*header
) +
391 if (total_size
!= fw
->size
)
392 return ERR_INVALID_SIZE
;
394 /* The checksum comes at the very end. */
395 chksum_ptr
= (void *)fw
->data
+ sizeof(*header
) + dmem_size
+ imem_size
;
397 init
->is_big_endian
= is_big_endian
;
398 init
->dmem_len
= dmem_size
;
399 init
->imem_len
= imem_size
;
400 init
->chksum
= (is_big_endian
)
401 ? be32_to_cpu(*chksum_ptr
) : le32_to_cpu(*chksum_ptr
);
406 /* Verify checksum by reading back the firmware from co-processor RAM. */
407 static int __verify_fw_checksum(struct init_data
*init
,
408 struct private_data
*priv
,
409 const struct dpfe_firmware_header
*header
,
412 u32 magic
, sequence
, version
, sum
;
413 u32 __iomem
*dmem
= priv
->dmem
;
414 u32 __iomem
*imem
= priv
->imem
;
417 if (init
->is_big_endian
) {
418 magic
= be32_to_cpu(header
->magic
);
419 sequence
= be32_to_cpu(header
->sequence
);
420 version
= be32_to_cpu(header
->version
);
422 magic
= le32_to_cpu(header
->magic
);
423 sequence
= le32_to_cpu(header
->sequence
);
424 version
= le32_to_cpu(header
->version
);
427 sum
= magic
+ sequence
+ version
+ init
->dmem_len
+ init
->imem_len
;
429 for (i
= 0; i
< init
->dmem_len
/ sizeof(u32
); i
++)
430 sum
+= readl_relaxed(dmem
+ i
);
432 for (i
= 0; i
< init
->imem_len
/ sizeof(u32
); i
++)
433 sum
+= readl_relaxed(imem
+ i
);
435 return (sum
== checksum
) ? 0 : -1;
438 static int __write_firmware(u32 __iomem
*mem
, const u32
*fw
,
439 unsigned int size
, bool is_big_endian
)
443 /* Convert size to 32-bit words. */
446 /* It is recommended to clear the firmware area first. */
447 for (i
= 0; i
< size
; i
++)
448 writel_relaxed(0, mem
+ i
);
452 for (i
= 0; i
< size
; i
++)
453 writel_relaxed(be32_to_cpu(fw
[i
]), mem
+ i
);
455 for (i
= 0; i
< size
; i
++)
456 writel_relaxed(le32_to_cpu(fw
[i
]), mem
+ i
);
462 static int brcmstb_dpfe_download_firmware(struct platform_device
*pdev
,
463 struct init_data
*init
)
465 const struct dpfe_firmware_header
*header
;
466 unsigned int dmem_size
, imem_size
;
467 struct device
*dev
= &pdev
->dev
;
468 bool is_big_endian
= false;
469 struct private_data
*priv
;
470 const struct firmware
*fw
;
471 const u32
*dmem
, *imem
;
475 priv
= platform_get_drvdata(pdev
);
478 * Skip downloading the firmware if the DCPU is already running and
479 * responding to commands.
481 if (is_dcpu_enabled(priv
->regs
)) {
482 u32 response
[MSG_FIELD_MAX
];
484 ret
= __send_command(priv
, DPFE_CMD_GET_INFO
, response
);
489 ret
= request_firmware(&fw
, FIRMWARE_NAME
, dev
);
490 /* request_firmware() prints its own error messages. */
494 ret
= __verify_firmware(init
, fw
);
498 __disable_dcpu(priv
->regs
);
500 is_big_endian
= init
->is_big_endian
;
501 dmem_size
= init
->dmem_len
;
502 imem_size
= init
->imem_len
;
504 /* At the beginning of the firmware blob is a header. */
505 header
= (struct dpfe_firmware_header
*)fw
->data
;
506 /* Void pointer to the beginning of the actual firmware. */
507 fw_blob
= fw
->data
+ sizeof(*header
);
508 /* IMEM comes right after the header. */
510 /* DMEM follows after IMEM. */
511 dmem
= fw_blob
+ imem_size
;
513 ret
= __write_firmware(priv
->dmem
, dmem
, dmem_size
, is_big_endian
);
516 ret
= __write_firmware(priv
->imem
, imem
, imem_size
, is_big_endian
);
520 ret
= __verify_fw_checksum(init
, priv
, header
, init
->chksum
);
524 __enable_dcpu(priv
->regs
);
529 static ssize_t
generic_show(unsigned int command
, u32 response
[],
530 struct device
*dev
, char *buf
)
532 struct private_data
*priv
;
535 priv
= dev_get_drvdata(dev
);
537 return sprintf(buf
, "ERROR: driver private data not set\n");
539 ret
= __send_command(priv
, command
, response
);
541 return sprintf(buf
, "ERROR: %s\n", error_text
[-ret
]);
546 static ssize_t
show_info(struct device
*dev
, struct device_attribute
*devattr
,
549 u32 response
[MSG_FIELD_MAX
];
553 ret
= generic_show(DPFE_CMD_GET_INFO
, response
, dev
, buf
);
557 info
= response
[MSG_ARG0
];
559 return sprintf(buf
, "%u.%u.%u.%u\n",
566 static ssize_t
show_refresh(struct device
*dev
,
567 struct device_attribute
*devattr
, char *buf
)
569 u32 response
[MSG_FIELD_MAX
];
571 struct private_data
*priv
;
572 u8 refresh
, sr_abort
, ppre
, thermal_offs
, tuf
;
576 ret
= generic_show(DPFE_CMD_GET_REFRESH
, response
, dev
, buf
);
580 priv
= dev_get_drvdata(dev
);
582 info
= get_msg_ptr(priv
, response
[MSG_ARG0
], buf
, &ret
);
586 mr4
= readl_relaxed(info
+ DRAM_INFO_MR4
) & DRAM_INFO_MR4_MASK
;
588 refresh
= (mr4
>> DRAM_MR4_REFRESH
) & DRAM_MR4_REFRESH_MASK
;
589 sr_abort
= (mr4
>> DRAM_MR4_SR_ABORT
) & DRAM_MR4_SR_ABORT_MASK
;
590 ppre
= (mr4
>> DRAM_MR4_PPRE
) & DRAM_MR4_PPRE_MASK
;
591 thermal_offs
= (mr4
>> DRAM_MR4_TH_OFFS
) & DRAM_MR4_TH_OFFS_MASK
;
592 tuf
= (mr4
>> DRAM_MR4_TUF
) & DRAM_MR4_TUF_MASK
;
594 return sprintf(buf
, "%#x %#x %#x %#x %#x %#x %#x\n",
595 readl_relaxed(info
+ DRAM_INFO_INTERVAL
),
596 refresh
, sr_abort
, ppre
, thermal_offs
, tuf
,
597 readl_relaxed(info
+ DRAM_INFO_ERROR
));
600 static ssize_t
store_refresh(struct device
*dev
, struct device_attribute
*attr
,
601 const char *buf
, size_t count
)
603 u32 response
[MSG_FIELD_MAX
];
604 struct private_data
*priv
;
609 if (kstrtoul(buf
, 0, &val
) < 0)
612 priv
= dev_get_drvdata(dev
);
614 ret
= __send_command(priv
, DPFE_CMD_GET_REFRESH
, response
);
618 info
= get_msg_ptr(priv
, response
[MSG_ARG0
], NULL
, NULL
);
622 writel_relaxed(val
, info
+ DRAM_INFO_INTERVAL
);
627 static ssize_t
show_vendor(struct device
*dev
, struct device_attribute
*devattr
,
630 u32 response
[MSG_FIELD_MAX
];
631 struct private_data
*priv
;
635 ret
= generic_show(DPFE_CMD_GET_VENDOR
, response
, dev
, buf
);
639 priv
= dev_get_drvdata(dev
);
641 info
= get_msg_ptr(priv
, response
[MSG_ARG0
], buf
, &ret
);
645 return sprintf(buf
, "%#x %#x %#x %#x %#x\n",
646 readl_relaxed(info
+ DRAM_VENDOR_MR5
) & DRAM_VENDOR_MASK
,
647 readl_relaxed(info
+ DRAM_VENDOR_MR6
) & DRAM_VENDOR_MASK
,
648 readl_relaxed(info
+ DRAM_VENDOR_MR7
) & DRAM_VENDOR_MASK
,
649 readl_relaxed(info
+ DRAM_VENDOR_MR8
) & DRAM_VENDOR_MASK
,
650 readl_relaxed(info
+ DRAM_VENDOR_ERROR
) &
654 static int brcmstb_dpfe_resume(struct platform_device
*pdev
)
656 struct init_data init
;
658 return brcmstb_dpfe_download_firmware(pdev
, &init
);
661 static DEVICE_ATTR(dpfe_info
, 0444, show_info
, NULL
);
662 static DEVICE_ATTR(dpfe_refresh
, 0644, show_refresh
, store_refresh
);
663 static DEVICE_ATTR(dpfe_vendor
, 0444, show_vendor
, NULL
);
664 static struct attribute
*dpfe_attrs
[] = {
665 &dev_attr_dpfe_info
.attr
,
666 &dev_attr_dpfe_refresh
.attr
,
667 &dev_attr_dpfe_vendor
.attr
,
670 ATTRIBUTE_GROUPS(dpfe
);
672 static int brcmstb_dpfe_probe(struct platform_device
*pdev
)
674 struct device
*dev
= &pdev
->dev
;
675 struct private_data
*priv
;
676 struct init_data init
;
677 struct resource
*res
;
680 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
684 mutex_init(&priv
->lock
);
685 platform_set_drvdata(pdev
, priv
);
687 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-cpu");
688 priv
->regs
= devm_ioremap_resource(dev
, res
);
689 if (IS_ERR(priv
->regs
)) {
690 dev_err(dev
, "couldn't map DCPU registers\n");
694 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-dmem");
695 priv
->dmem
= devm_ioremap_resource(dev
, res
);
696 if (IS_ERR(priv
->dmem
)) {
697 dev_err(dev
, "Couldn't map DCPU data memory\n");
701 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dpfe-imem");
702 priv
->imem
= devm_ioremap_resource(dev
, res
);
703 if (IS_ERR(priv
->imem
)) {
704 dev_err(dev
, "Couldn't map DCPU instruction memory\n");
708 ret
= brcmstb_dpfe_download_firmware(pdev
, &init
);
712 ret
= sysfs_create_groups(&pdev
->dev
.kobj
, dpfe_groups
);
714 dev_info(dev
, "registered.\n");
719 static int brcmstb_dpfe_remove(struct platform_device
*pdev
)
721 sysfs_remove_groups(&pdev
->dev
.kobj
, dpfe_groups
);
726 static const struct of_device_id brcmstb_dpfe_of_match
[] = {
727 { .compatible
= "brcm,dpfe-cpu", },
730 MODULE_DEVICE_TABLE(of
, brcmstb_dpfe_of_match
);
732 static struct platform_driver brcmstb_dpfe_driver
= {
735 .of_match_table
= brcmstb_dpfe_of_match
,
737 .probe
= brcmstb_dpfe_probe
,
738 .remove
= brcmstb_dpfe_remove
,
739 .resume
= brcmstb_dpfe_resume
,
742 module_platform_driver(brcmstb_dpfe_driver
);
744 MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
745 MODULE_DESCRIPTION("BRCMSTB DDR PHY Front End Driver");
746 MODULE_LICENSE("GPL");