blob: 9ad5ea08c134e30e593a68ea8ef0a57c5c4e5d14 [file] [log] [blame]
Markus Mayer2f330ca2017-08-24 16:36:26 -07001/*
2 * DDR PHY Front End (DPFE) driver for Broadcom set top box SoCs
3 *
4 * Copyright (c) 2017 Broadcom
5 *
6 * Released under the GPLv2 only.
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10/*
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
16 * RAM.
17 *
18 * Throughout the driver, we use readl_relaxed() and writel_relaxed(), which
19 * already contain the appropriate le32_to_cpu()/cpu_to_le32() calls.
20 *
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
27 *
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.
31 */
32
33#include <linux/delay.h>
34#include <linux/firmware.h>
35#include <linux/io.h>
36#include <linux/module.h>
37#include <linux/of_address.h>
Markus Mayer58a84992019-04-02 16:01:01 -070038#include <linux/of_device.h>
Markus Mayer2f330ca2017-08-24 16:36:26 -070039#include <linux/platform_device.h>
40
41#define DRVNAME "brcmstb-dpfe"
Markus Mayer2f330ca2017-08-24 16:36:26 -070042
43/* DCPU register offsets */
44#define REG_DCPU_RESET 0x0
45#define REG_TO_DCPU_MBOX 0x10
46#define REG_TO_HOST_MBOX 0x14
47
Markus Mayerfee5f1e2018-02-13 12:40:40 -080048/* 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)
54
Markus Mayer2f330ca2017-08-24 16:36:26 -070055/* Message RAM */
Markus Mayerfee5f1e2018-02-13 12:40:40 -080056#define DCPU_MSG_RAM_START 0x100
57#define DCPU_MSG_RAM(x) (DCPU_MSG_RAM_START + (x) * sizeof(u32))
Markus Mayer2f330ca2017-08-24 16:36:26 -070058
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
Markus Mayer78a6f5b2019-02-11 17:24:43 -080064#define DRAM_INFO_MR4_SHIFT 24 /* We need to look at byte 3 */
Markus Mayer2f330ca2017-08-24 16:36:26 -070065
66/* DRAM MR4 Offsets & Masks */
67#define DRAM_MR4_REFRESH 0x0 /* Refresh rate */
68#define DRAM_MR4_SR_ABORT 0x3 /* Self Refresh Abort */
69#define DRAM_MR4_PPRE 0x4 /* Post-package repair entry/exit */
70#define DRAM_MR4_TH_OFFS 0x5 /* Thermal Offset; vendor specific */
71#define DRAM_MR4_TUF 0x7 /* Temperature Update Flag */
72
73#define DRAM_MR4_REFRESH_MASK 0x7
74#define DRAM_MR4_SR_ABORT_MASK 0x1
75#define DRAM_MR4_PPRE_MASK 0x1
76#define DRAM_MR4_TH_OFFS_MASK 0x3
77#define DRAM_MR4_TUF_MASK 0x1
78
79/* DRAM Vendor Offsets & Masks */
80#define DRAM_VENDOR_MR5 0x0
81#define DRAM_VENDOR_MR6 0x4
82#define DRAM_VENDOR_MR7 0x8
83#define DRAM_VENDOR_MR8 0xc
84#define DRAM_VENDOR_ERROR 0x10
85#define DRAM_VENDOR_MASK 0xff
Markus Mayer78a6f5b2019-02-11 17:24:43 -080086#define DRAM_VENDOR_SHIFT 24 /* We need to look at byte 3 */
Markus Mayer2f330ca2017-08-24 16:36:26 -070087
88/* Reset register bits & masks */
89#define DCPU_RESET_SHIFT 0x0
90#define DCPU_RESET_MASK 0x1
91#define DCPU_CLK_DISABLE_SHIFT 0x2
92
93/* DCPU return codes */
94#define DCPU_RET_ERROR_BIT BIT(31)
95#define DCPU_RET_SUCCESS 0x1
96#define DCPU_RET_ERR_HEADER (DCPU_RET_ERROR_BIT | BIT(0))
97#define DCPU_RET_ERR_INVAL (DCPU_RET_ERROR_BIT | BIT(1))
98#define DCPU_RET_ERR_CHKSUM (DCPU_RET_ERROR_BIT | BIT(2))
99#define DCPU_RET_ERR_COMMAND (DCPU_RET_ERROR_BIT | BIT(3))
100/* This error code is not firmware defined and only used in the driver. */
101#define DCPU_RET_ERR_TIMEDOUT (DCPU_RET_ERROR_BIT | BIT(4))
102
103/* Firmware magic */
104#define DPFE_BE_MAGIC 0xfe1010fe
105#define DPFE_LE_MAGIC 0xfe0101fe
106
107/* Error codes */
108#define ERR_INVALID_MAGIC -1
109#define ERR_INVALID_SIZE -2
110#define ERR_INVALID_CHKSUM -3
111
112/* Message types */
113#define DPFE_MSG_TYPE_COMMAND 1
114#define DPFE_MSG_TYPE_RESPONSE 2
115
Markus Mayer7ccd2ff2019-02-11 17:24:41 -0800116#define DELAY_LOOP_MAX 1000
Markus Mayer2f330ca2017-08-24 16:36:26 -0700117
118enum dpfe_msg_fields {
119 MSG_HEADER,
120 MSG_COMMAND,
121 MSG_ARG_COUNT,
122 MSG_ARG0,
123 MSG_CHKSUM,
124 MSG_FIELD_MAX /* Last entry */
125};
126
127enum dpfe_commands {
128 DPFE_CMD_GET_INFO,
129 DPFE_CMD_GET_REFRESH,
130 DPFE_CMD_GET_VENDOR,
131 DPFE_CMD_MAX /* Last entry */
132};
133
Markus Mayer2f330ca2017-08-24 16:36:26 -0700134/*
135 * Format of the binary firmware file:
136 *
137 * entry
138 * 0 header
139 * value: 0xfe0101fe <== little endian
140 * 0xfe1010fe <== big endian
141 * 1 sequence:
142 * [31:16] total segments on this build
143 * [15:0] this segment sequence.
144 * 2 FW version
145 * 3 IMEM byte size
146 * 4 DMEM byte size
147 * IMEM
148 * DMEM
149 * last checksum ==> sum of everything
150 */
151struct dpfe_firmware_header {
152 u32 magic;
153 u32 sequence;
154 u32 version;
155 u32 imem_size;
156 u32 dmem_size;
157};
158
159/* Things we only need during initialization. */
160struct init_data {
161 unsigned int dmem_len;
162 unsigned int imem_len;
163 unsigned int chksum;
164 bool is_big_endian;
165};
166
Markus Mayer58a84992019-04-02 16:01:01 -0700167/* API version and corresponding commands */
168struct dpfe_api {
169 int version;
170 const char *fw_name;
171 u32 command[DPFE_CMD_MAX][MSG_FIELD_MAX];
172};
173
Markus Mayer2f330ca2017-08-24 16:36:26 -0700174/* Things we need for as long as we are active. */
175struct private_data {
176 void __iomem *regs;
177 void __iomem *dmem;
178 void __iomem *imem;
179 struct device *dev;
Markus Mayer58a84992019-04-02 16:01:01 -0700180 const struct dpfe_api *dpfe_api;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700181 struct mutex lock;
182};
183
184static const char *error_text[] = {
185 "Success", "Header code incorrect", "Unknown command or argument",
186 "Incorrect checksum", "Malformed command", "Timed out",
187};
188
Markus Mayer58a84992019-04-02 16:01:01 -0700189/* API v2 firmware commands */
190static const struct dpfe_api dpfe_api_v2 = {
191 .version = 2,
192 .fw_name = "dpfe.bin",
193 .command = {
194 [DPFE_CMD_GET_INFO] = {
195 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
196 [MSG_COMMAND] = 1,
197 [MSG_ARG_COUNT] = 1,
198 [MSG_ARG0] = 1,
199 [MSG_CHKSUM] = 4,
200 },
201 [DPFE_CMD_GET_REFRESH] = {
202 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
203 [MSG_COMMAND] = 2,
204 [MSG_ARG_COUNT] = 1,
205 [MSG_ARG0] = 1,
206 [MSG_CHKSUM] = 5,
207 },
208 [DPFE_CMD_GET_VENDOR] = {
209 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
210 [MSG_COMMAND] = 2,
211 [MSG_ARG_COUNT] = 1,
212 [MSG_ARG0] = 2,
213 [MSG_CHKSUM] = 6,
214 },
215 }
Markus Mayer2f330ca2017-08-24 16:36:26 -0700216};
217
Markus Mayerd56e7462017-10-02 16:13:46 -0700218static bool is_dcpu_enabled(void __iomem *regs)
219{
220 u32 val;
221
222 val = readl_relaxed(regs + REG_DCPU_RESET);
223
224 return !(val & DCPU_RESET_MASK);
225}
226
Markus Mayer2f330ca2017-08-24 16:36:26 -0700227static void __disable_dcpu(void __iomem *regs)
228{
229 u32 val;
230
Markus Mayerd56e7462017-10-02 16:13:46 -0700231 if (!is_dcpu_enabled(regs))
232 return;
233
234 /* Put DCPU in reset if it's running. */
Markus Mayer2f330ca2017-08-24 16:36:26 -0700235 val = readl_relaxed(regs + REG_DCPU_RESET);
Markus Mayerd56e7462017-10-02 16:13:46 -0700236 val |= (1 << DCPU_RESET_SHIFT);
237 writel_relaxed(val, regs + REG_DCPU_RESET);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700238}
239
240static void __enable_dcpu(void __iomem *regs)
241{
242 u32 val;
243
244 /* Clear mailbox registers. */
245 writel_relaxed(0, regs + REG_TO_DCPU_MBOX);
246 writel_relaxed(0, regs + REG_TO_HOST_MBOX);
247
248 /* Disable DCPU clock gating */
249 val = readl_relaxed(regs + REG_DCPU_RESET);
250 val &= ~(1 << DCPU_CLK_DISABLE_SHIFT);
251 writel_relaxed(val, regs + REG_DCPU_RESET);
252
253 /* Take DCPU out of reset */
254 val = readl_relaxed(regs + REG_DCPU_RESET);
255 val &= ~(1 << DCPU_RESET_SHIFT);
256 writel_relaxed(val, regs + REG_DCPU_RESET);
257}
258
259static unsigned int get_msg_chksum(const u32 msg[])
260{
261 unsigned int sum = 0;
262 unsigned int i;
263
264 /* Don't include the last field in the checksum. */
265 for (i = 0; i < MSG_FIELD_MAX - 1; i++)
266 sum += msg[i];
267
268 return sum;
269}
270
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800271static void __iomem *get_msg_ptr(struct private_data *priv, u32 response,
272 char *buf, ssize_t *size)
273{
274 unsigned int msg_type;
275 unsigned int offset;
276 void __iomem *ptr = NULL;
277
278 msg_type = (response >> DRAM_MSG_TYPE_OFFSET) & DRAM_MSG_TYPE_MASK;
279 offset = (response >> DRAM_MSG_ADDR_OFFSET) & DRAM_MSG_ADDR_MASK;
280
281 /*
282 * msg_type == 1: the offset is relative to the message RAM
283 * msg_type == 0: the offset is relative to the data RAM (this is the
284 * previous way of passing data)
285 * msg_type is anything else: there's critical hardware problem
286 */
287 switch (msg_type) {
288 case 1:
289 ptr = priv->regs + DCPU_MSG_RAM_START + offset;
290 break;
291 case 0:
292 ptr = priv->dmem + offset;
293 break;
294 default:
295 dev_emerg(priv->dev, "invalid message reply from DCPU: %#x\n",
296 response);
297 if (buf && size)
298 *size = sprintf(buf,
299 "FATAL: communication error with DCPU\n");
300 }
301
302 return ptr;
303}
304
Markus Mayer2f330ca2017-08-24 16:36:26 -0700305static int __send_command(struct private_data *priv, unsigned int cmd,
306 u32 result[])
307{
Markus Mayer58a84992019-04-02 16:01:01 -0700308 const u32 *msg = priv->dpfe_api->command[cmd];
Markus Mayer2f330ca2017-08-24 16:36:26 -0700309 void __iomem *regs = priv->regs;
310 unsigned int i, chksum;
311 int ret = 0;
312 u32 resp;
313
314 if (cmd >= DPFE_CMD_MAX)
315 return -1;
316
317 mutex_lock(&priv->lock);
318
Markus Mayera7c25752019-04-02 16:01:00 -0700319 /* Wait for DCPU to become ready */
320 for (i = 0; i < DELAY_LOOP_MAX; i++) {
321 resp = readl_relaxed(regs + REG_TO_HOST_MBOX);
322 if (resp == 0)
323 break;
324 msleep(1);
325 }
326 if (resp != 0) {
327 mutex_unlock(&priv->lock);
328 return -ETIMEDOUT;
329 }
330
Markus Mayer2f330ca2017-08-24 16:36:26 -0700331 /* Write command and arguments to message area */
332 for (i = 0; i < MSG_FIELD_MAX; i++)
333 writel_relaxed(msg[i], regs + DCPU_MSG_RAM(i));
334
335 /* Tell DCPU there is a command waiting */
336 writel_relaxed(1, regs + REG_TO_DCPU_MBOX);
337
338 /* Wait for DCPU to process the command */
339 for (i = 0; i < DELAY_LOOP_MAX; i++) {
340 /* Read response code */
341 resp = readl_relaxed(regs + REG_TO_HOST_MBOX);
342 if (resp > 0)
343 break;
Markus Mayer7ccd2ff2019-02-11 17:24:41 -0800344 msleep(1);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700345 }
346
347 if (i == DELAY_LOOP_MAX) {
348 resp = (DCPU_RET_ERR_TIMEDOUT & ~DCPU_RET_ERROR_BIT);
349 ret = -ffs(resp);
350 } else {
351 /* Read response data */
352 for (i = 0; i < MSG_FIELD_MAX; i++)
353 result[i] = readl_relaxed(regs + DCPU_MSG_RAM(i));
354 }
355
356 /* Tell DCPU we are done */
357 writel_relaxed(0, regs + REG_TO_HOST_MBOX);
358
359 mutex_unlock(&priv->lock);
360
361 if (ret)
362 return ret;
363
364 /* Verify response */
365 chksum = get_msg_chksum(result);
366 if (chksum != result[MSG_CHKSUM])
367 resp = DCPU_RET_ERR_CHKSUM;
368
369 if (resp != DCPU_RET_SUCCESS) {
370 resp &= ~DCPU_RET_ERROR_BIT;
371 ret = -ffs(resp);
372 }
373
374 return ret;
375}
376
377/* Ensure that the firmware file loaded meets all the requirements. */
378static int __verify_firmware(struct init_data *init,
379 const struct firmware *fw)
380{
381 const struct dpfe_firmware_header *header = (void *)fw->data;
382 unsigned int dmem_size, imem_size, total_size;
383 bool is_big_endian = false;
384 const u32 *chksum_ptr;
385
386 if (header->magic == DPFE_BE_MAGIC)
387 is_big_endian = true;
388 else if (header->magic != DPFE_LE_MAGIC)
389 return ERR_INVALID_MAGIC;
390
391 if (is_big_endian) {
392 dmem_size = be32_to_cpu(header->dmem_size);
393 imem_size = be32_to_cpu(header->imem_size);
394 } else {
395 dmem_size = le32_to_cpu(header->dmem_size);
396 imem_size = le32_to_cpu(header->imem_size);
397 }
398
399 /* Data and instruction sections are 32 bit words. */
400 if ((dmem_size % sizeof(u32)) != 0 || (imem_size % sizeof(u32)) != 0)
401 return ERR_INVALID_SIZE;
402
403 /*
404 * The header + the data section + the instruction section + the
405 * checksum must be equal to the total firmware size.
406 */
407 total_size = dmem_size + imem_size + sizeof(*header) +
408 sizeof(*chksum_ptr);
409 if (total_size != fw->size)
410 return ERR_INVALID_SIZE;
411
412 /* The checksum comes at the very end. */
413 chksum_ptr = (void *)fw->data + sizeof(*header) + dmem_size + imem_size;
414
415 init->is_big_endian = is_big_endian;
416 init->dmem_len = dmem_size;
417 init->imem_len = imem_size;
418 init->chksum = (is_big_endian)
419 ? be32_to_cpu(*chksum_ptr) : le32_to_cpu(*chksum_ptr);
420
421 return 0;
422}
423
424/* Verify checksum by reading back the firmware from co-processor RAM. */
425static int __verify_fw_checksum(struct init_data *init,
426 struct private_data *priv,
427 const struct dpfe_firmware_header *header,
428 u32 checksum)
429{
430 u32 magic, sequence, version, sum;
431 u32 __iomem *dmem = priv->dmem;
432 u32 __iomem *imem = priv->imem;
433 unsigned int i;
434
435 if (init->is_big_endian) {
436 magic = be32_to_cpu(header->magic);
437 sequence = be32_to_cpu(header->sequence);
438 version = be32_to_cpu(header->version);
439 } else {
440 magic = le32_to_cpu(header->magic);
441 sequence = le32_to_cpu(header->sequence);
442 version = le32_to_cpu(header->version);
443 }
444
445 sum = magic + sequence + version + init->dmem_len + init->imem_len;
446
447 for (i = 0; i < init->dmem_len / sizeof(u32); i++)
448 sum += readl_relaxed(dmem + i);
449
450 for (i = 0; i < init->imem_len / sizeof(u32); i++)
451 sum += readl_relaxed(imem + i);
452
453 return (sum == checksum) ? 0 : -1;
454}
455
456static int __write_firmware(u32 __iomem *mem, const u32 *fw,
457 unsigned int size, bool is_big_endian)
458{
459 unsigned int i;
460
461 /* Convert size to 32-bit words. */
462 size /= sizeof(u32);
463
464 /* It is recommended to clear the firmware area first. */
465 for (i = 0; i < size; i++)
466 writel_relaxed(0, mem + i);
467
468 /* Now copy it. */
469 if (is_big_endian) {
470 for (i = 0; i < size; i++)
471 writel_relaxed(be32_to_cpu(fw[i]), mem + i);
472 } else {
473 for (i = 0; i < size; i++)
474 writel_relaxed(le32_to_cpu(fw[i]), mem + i);
475 }
476
477 return 0;
478}
479
480static int brcmstb_dpfe_download_firmware(struct platform_device *pdev,
481 struct init_data *init)
482{
483 const struct dpfe_firmware_header *header;
484 unsigned int dmem_size, imem_size;
485 struct device *dev = &pdev->dev;
486 bool is_big_endian = false;
487 struct private_data *priv;
488 const struct firmware *fw;
489 const u32 *dmem, *imem;
490 const void *fw_blob;
491 int ret;
492
Markus Mayera56d3392017-10-02 16:13:47 -0700493 priv = platform_get_drvdata(pdev);
494
495 /*
496 * Skip downloading the firmware if the DCPU is already running and
497 * responding to commands.
498 */
499 if (is_dcpu_enabled(priv->regs)) {
500 u32 response[MSG_FIELD_MAX];
501
502 ret = __send_command(priv, DPFE_CMD_GET_INFO, response);
503 if (!ret)
504 return 0;
505 }
506
Markus Mayer58a84992019-04-02 16:01:01 -0700507 /*
508 * If the firmware filename is NULL it means the boot firmware has to
509 * download the DCPU firmware for us. If that didn't work, we have to
510 * bail, since downloading it ourselves wouldn't work either.
511 */
512 if (!priv->dpfe_api->fw_name)
513 return -ENODEV;
514
515 ret = request_firmware(&fw, priv->dpfe_api->fw_name, dev);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700516 /* request_firmware() prints its own error messages. */
517 if (ret)
518 return ret;
519
Markus Mayer2f330ca2017-08-24 16:36:26 -0700520 ret = __verify_firmware(init, fw);
521 if (ret)
522 return -EFAULT;
523
524 __disable_dcpu(priv->regs);
525
526 is_big_endian = init->is_big_endian;
527 dmem_size = init->dmem_len;
528 imem_size = init->imem_len;
529
530 /* At the beginning of the firmware blob is a header. */
531 header = (struct dpfe_firmware_header *)fw->data;
532 /* Void pointer to the beginning of the actual firmware. */
533 fw_blob = fw->data + sizeof(*header);
534 /* IMEM comes right after the header. */
535 imem = fw_blob;
536 /* DMEM follows after IMEM. */
537 dmem = fw_blob + imem_size;
538
539 ret = __write_firmware(priv->dmem, dmem, dmem_size, is_big_endian);
540 if (ret)
541 return ret;
542 ret = __write_firmware(priv->imem, imem, imem_size, is_big_endian);
543 if (ret)
544 return ret;
545
546 ret = __verify_fw_checksum(init, priv, header, init->chksum);
547 if (ret)
548 return ret;
549
550 __enable_dcpu(priv->regs);
551
552 return 0;
553}
554
555static ssize_t generic_show(unsigned int command, u32 response[],
Markus Mayer900c8f52019-02-11 17:24:42 -0800556 struct private_data *priv, char *buf)
Markus Mayer2f330ca2017-08-24 16:36:26 -0700557{
Markus Mayer2f330ca2017-08-24 16:36:26 -0700558 int ret;
559
Markus Mayer2f330ca2017-08-24 16:36:26 -0700560 if (!priv)
561 return sprintf(buf, "ERROR: driver private data not set\n");
562
563 ret = __send_command(priv, command, response);
564 if (ret < 0)
565 return sprintf(buf, "ERROR: %s\n", error_text[-ret]);
566
567 return 0;
568}
569
570static ssize_t show_info(struct device *dev, struct device_attribute *devattr,
571 char *buf)
572{
573 u32 response[MSG_FIELD_MAX];
Markus Mayer900c8f52019-02-11 17:24:42 -0800574 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700575 unsigned int info;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800576 ssize_t ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700577
Markus Mayer900c8f52019-02-11 17:24:42 -0800578 priv = dev_get_drvdata(dev);
579 ret = generic_show(DPFE_CMD_GET_INFO, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700580 if (ret)
581 return ret;
582
583 info = response[MSG_ARG0];
584
585 return sprintf(buf, "%u.%u.%u.%u\n",
586 (info >> 24) & 0xff,
587 (info >> 16) & 0xff,
588 (info >> 8) & 0xff,
589 info & 0xff);
590}
591
592static ssize_t show_refresh(struct device *dev,
593 struct device_attribute *devattr, char *buf)
594{
595 u32 response[MSG_FIELD_MAX];
596 void __iomem *info;
597 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700598 u8 refresh, sr_abort, ppre, thermal_offs, tuf;
599 u32 mr4;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800600 ssize_t ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700601
Markus Mayer900c8f52019-02-11 17:24:42 -0800602 priv = dev_get_drvdata(dev);
603 ret = generic_show(DPFE_CMD_GET_REFRESH, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700604 if (ret)
605 return ret;
606
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800607 info = get_msg_ptr(priv, response[MSG_ARG0], buf, &ret);
608 if (!info)
609 return ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700610
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800611 mr4 = (readl_relaxed(info + DRAM_INFO_MR4) >> DRAM_INFO_MR4_SHIFT) &
Markus Mayer1ffc0b52019-04-02 16:00:58 -0700612 DRAM_INFO_MR4_MASK;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700613
614 refresh = (mr4 >> DRAM_MR4_REFRESH) & DRAM_MR4_REFRESH_MASK;
615 sr_abort = (mr4 >> DRAM_MR4_SR_ABORT) & DRAM_MR4_SR_ABORT_MASK;
616 ppre = (mr4 >> DRAM_MR4_PPRE) & DRAM_MR4_PPRE_MASK;
617 thermal_offs = (mr4 >> DRAM_MR4_TH_OFFS) & DRAM_MR4_TH_OFFS_MASK;
618 tuf = (mr4 >> DRAM_MR4_TUF) & DRAM_MR4_TUF_MASK;
619
620 return sprintf(buf, "%#x %#x %#x %#x %#x %#x %#x\n",
621 readl_relaxed(info + DRAM_INFO_INTERVAL),
622 refresh, sr_abort, ppre, thermal_offs, tuf,
623 readl_relaxed(info + DRAM_INFO_ERROR));
624}
625
626static ssize_t store_refresh(struct device *dev, struct device_attribute *attr,
627 const char *buf, size_t count)
628{
629 u32 response[MSG_FIELD_MAX];
630 struct private_data *priv;
631 void __iomem *info;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700632 unsigned long val;
633 int ret;
634
635 if (kstrtoul(buf, 0, &val) < 0)
636 return -EINVAL;
637
638 priv = dev_get_drvdata(dev);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700639 ret = __send_command(priv, DPFE_CMD_GET_REFRESH, response);
640 if (ret)
641 return ret;
642
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800643 info = get_msg_ptr(priv, response[MSG_ARG0], NULL, NULL);
644 if (!info)
645 return -EIO;
646
Markus Mayer2f330ca2017-08-24 16:36:26 -0700647 writel_relaxed(val, info + DRAM_INFO_INTERVAL);
648
649 return count;
650}
651
652static ssize_t show_vendor(struct device *dev, struct device_attribute *devattr,
Markus Mayer1ffc0b52019-04-02 16:00:58 -0700653 char *buf)
Markus Mayer2f330ca2017-08-24 16:36:26 -0700654{
655 u32 response[MSG_FIELD_MAX];
656 struct private_data *priv;
657 void __iomem *info;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800658 ssize_t ret;
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800659 u32 mr5, mr6, mr7, mr8, err;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700660
Markus Mayer900c8f52019-02-11 17:24:42 -0800661 priv = dev_get_drvdata(dev);
662 ret = generic_show(DPFE_CMD_GET_VENDOR, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700663 if (ret)
664 return ret;
665
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800666 info = get_msg_ptr(priv, response[MSG_ARG0], buf, &ret);
667 if (!info)
668 return ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700669
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800670 mr5 = (readl_relaxed(info + DRAM_VENDOR_MR5) >> DRAM_VENDOR_SHIFT) &
671 DRAM_VENDOR_MASK;
672 mr6 = (readl_relaxed(info + DRAM_VENDOR_MR6) >> DRAM_VENDOR_SHIFT) &
673 DRAM_VENDOR_MASK;
674 mr7 = (readl_relaxed(info + DRAM_VENDOR_MR7) >> DRAM_VENDOR_SHIFT) &
675 DRAM_VENDOR_MASK;
676 mr8 = (readl_relaxed(info + DRAM_VENDOR_MR8) >> DRAM_VENDOR_SHIFT) &
677 DRAM_VENDOR_MASK;
678 err = readl_relaxed(info + DRAM_VENDOR_ERROR) & DRAM_VENDOR_MASK;
679
680 return sprintf(buf, "%#x %#x %#x %#x %#x\n", mr5, mr6, mr7, mr8, err);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700681}
682
683static int brcmstb_dpfe_resume(struct platform_device *pdev)
684{
685 struct init_data init;
686
687 return brcmstb_dpfe_download_firmware(pdev, &init);
688}
689
690static DEVICE_ATTR(dpfe_info, 0444, show_info, NULL);
691static DEVICE_ATTR(dpfe_refresh, 0644, show_refresh, store_refresh);
692static DEVICE_ATTR(dpfe_vendor, 0444, show_vendor, NULL);
693static struct attribute *dpfe_attrs[] = {
694 &dev_attr_dpfe_info.attr,
695 &dev_attr_dpfe_refresh.attr,
696 &dev_attr_dpfe_vendor.attr,
697 NULL
698};
699ATTRIBUTE_GROUPS(dpfe);
700
701static int brcmstb_dpfe_probe(struct platform_device *pdev)
702{
703 struct device *dev = &pdev->dev;
704 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700705 struct init_data init;
706 struct resource *res;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700707 int ret;
708
709 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
710 if (!priv)
711 return -ENOMEM;
712
713 mutex_init(&priv->lock);
714 platform_set_drvdata(pdev, priv);
715
Markus Mayer2f330ca2017-08-24 16:36:26 -0700716 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-cpu");
717 priv->regs = devm_ioremap_resource(dev, res);
718 if (IS_ERR(priv->regs)) {
719 dev_err(dev, "couldn't map DCPU registers\n");
720 return -ENODEV;
721 }
722
723 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-dmem");
724 priv->dmem = devm_ioremap_resource(dev, res);
725 if (IS_ERR(priv->dmem)) {
726 dev_err(dev, "Couldn't map DCPU data memory\n");
727 return -ENOENT;
728 }
729
730 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-imem");
731 priv->imem = devm_ioremap_resource(dev, res);
732 if (IS_ERR(priv->imem)) {
733 dev_err(dev, "Couldn't map DCPU instruction memory\n");
734 return -ENOENT;
735 }
736
Markus Mayer58a84992019-04-02 16:01:01 -0700737 priv->dpfe_api = of_device_get_match_data(dev);
738 if (unlikely(!priv->dpfe_api)) {
739 /*
740 * It should be impossible to end up here, but to be safe we
741 * check anyway.
742 */
743 dev_err(dev, "Couldn't determine API\n");
744 return -ENOENT;
745 }
746
Markus Mayer2f330ca2017-08-24 16:36:26 -0700747 ret = brcmstb_dpfe_download_firmware(pdev, &init);
Markus Mayer6ca5d2ba2019-04-02 16:00:59 -0700748 if (ret) {
749 dev_err(dev, "Couldn't download firmware -- %d\n", ret);
Florian Fainellib1d09732018-03-27 16:40:38 -0700750 return ret;
Markus Mayer6ca5d2ba2019-04-02 16:00:59 -0700751 }
Markus Mayer2f330ca2017-08-24 16:36:26 -0700752
Florian Fainellib1d09732018-03-27 16:40:38 -0700753 ret = sysfs_create_groups(&pdev->dev.kobj, dpfe_groups);
754 if (!ret)
Markus Mayer58a84992019-04-02 16:01:01 -0700755 dev_info(dev, "registered with API v%d.\n",
756 priv->dpfe_api->version);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700757
758 return ret;
759}
760
Florian Fainellib1d09732018-03-27 16:40:38 -0700761static int brcmstb_dpfe_remove(struct platform_device *pdev)
762{
763 sysfs_remove_groups(&pdev->dev.kobj, dpfe_groups);
764
765 return 0;
766}
767
Markus Mayer2f330ca2017-08-24 16:36:26 -0700768static const struct of_device_id brcmstb_dpfe_of_match[] = {
Markus Mayer58a84992019-04-02 16:01:01 -0700769 { .compatible = "brcm,dpfe-cpu", .data = &dpfe_api_v2 },
Markus Mayer2f330ca2017-08-24 16:36:26 -0700770 {}
771};
772MODULE_DEVICE_TABLE(of, brcmstb_dpfe_of_match);
773
774static struct platform_driver brcmstb_dpfe_driver = {
775 .driver = {
776 .name = DRVNAME,
777 .of_match_table = brcmstb_dpfe_of_match,
778 },
779 .probe = brcmstb_dpfe_probe,
Florian Fainellib1d09732018-03-27 16:40:38 -0700780 .remove = brcmstb_dpfe_remove,
Markus Mayer2f330ca2017-08-24 16:36:26 -0700781 .resume = brcmstb_dpfe_resume,
782};
783
784module_platform_driver(brcmstb_dpfe_driver);
785
786MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
787MODULE_DESCRIPTION("BRCMSTB DDR PHY Front End Driver");
788MODULE_LICENSE("GPL");