blob: f8d05a8266c35e16aef05451f64222e0b5399c16 [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>
38#include <linux/platform_device.h>
39
40#define DRVNAME "brcmstb-dpfe"
41#define FIRMWARE_NAME "dpfe.bin"
42
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
167/* Things we need for as long as we are active. */
168struct private_data {
169 void __iomem *regs;
170 void __iomem *dmem;
171 void __iomem *imem;
172 struct device *dev;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700173 struct mutex lock;
174};
175
176static const char *error_text[] = {
177 "Success", "Header code incorrect", "Unknown command or argument",
178 "Incorrect checksum", "Malformed command", "Timed out",
179};
180
181/* List of supported firmware commands */
182static const u32 dpfe_commands[DPFE_CMD_MAX][MSG_FIELD_MAX] = {
183 [DPFE_CMD_GET_INFO] = {
184 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
185 [MSG_COMMAND] = 1,
186 [MSG_ARG_COUNT] = 1,
187 [MSG_ARG0] = 1,
188 [MSG_CHKSUM] = 4,
189 },
190 [DPFE_CMD_GET_REFRESH] = {
191 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
192 [MSG_COMMAND] = 2,
193 [MSG_ARG_COUNT] = 1,
194 [MSG_ARG0] = 1,
195 [MSG_CHKSUM] = 5,
196 },
197 [DPFE_CMD_GET_VENDOR] = {
198 [MSG_HEADER] = DPFE_MSG_TYPE_COMMAND,
199 [MSG_COMMAND] = 2,
200 [MSG_ARG_COUNT] = 1,
201 [MSG_ARG0] = 2,
202 [MSG_CHKSUM] = 6,
203 },
204};
205
Markus Mayerd56e7462017-10-02 16:13:46 -0700206static bool is_dcpu_enabled(void __iomem *regs)
207{
208 u32 val;
209
210 val = readl_relaxed(regs + REG_DCPU_RESET);
211
212 return !(val & DCPU_RESET_MASK);
213}
214
Markus Mayer2f330ca2017-08-24 16:36:26 -0700215static void __disable_dcpu(void __iomem *regs)
216{
217 u32 val;
218
Markus Mayerd56e7462017-10-02 16:13:46 -0700219 if (!is_dcpu_enabled(regs))
220 return;
221
222 /* Put DCPU in reset if it's running. */
Markus Mayer2f330ca2017-08-24 16:36:26 -0700223 val = readl_relaxed(regs + REG_DCPU_RESET);
Markus Mayerd56e7462017-10-02 16:13:46 -0700224 val |= (1 << DCPU_RESET_SHIFT);
225 writel_relaxed(val, regs + REG_DCPU_RESET);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700226}
227
228static void __enable_dcpu(void __iomem *regs)
229{
230 u32 val;
231
232 /* Clear mailbox registers. */
233 writel_relaxed(0, regs + REG_TO_DCPU_MBOX);
234 writel_relaxed(0, regs + REG_TO_HOST_MBOX);
235
236 /* Disable DCPU clock gating */
237 val = readl_relaxed(regs + REG_DCPU_RESET);
238 val &= ~(1 << DCPU_CLK_DISABLE_SHIFT);
239 writel_relaxed(val, regs + REG_DCPU_RESET);
240
241 /* Take DCPU out of reset */
242 val = readl_relaxed(regs + REG_DCPU_RESET);
243 val &= ~(1 << DCPU_RESET_SHIFT);
244 writel_relaxed(val, regs + REG_DCPU_RESET);
245}
246
247static unsigned int get_msg_chksum(const u32 msg[])
248{
249 unsigned int sum = 0;
250 unsigned int i;
251
252 /* Don't include the last field in the checksum. */
253 for (i = 0; i < MSG_FIELD_MAX - 1; i++)
254 sum += msg[i];
255
256 return sum;
257}
258
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800259static void __iomem *get_msg_ptr(struct private_data *priv, u32 response,
260 char *buf, ssize_t *size)
261{
262 unsigned int msg_type;
263 unsigned int offset;
264 void __iomem *ptr = NULL;
265
266 msg_type = (response >> DRAM_MSG_TYPE_OFFSET) & DRAM_MSG_TYPE_MASK;
267 offset = (response >> DRAM_MSG_ADDR_OFFSET) & DRAM_MSG_ADDR_MASK;
268
269 /*
270 * msg_type == 1: the offset is relative to the message RAM
271 * msg_type == 0: the offset is relative to the data RAM (this is the
272 * previous way of passing data)
273 * msg_type is anything else: there's critical hardware problem
274 */
275 switch (msg_type) {
276 case 1:
277 ptr = priv->regs + DCPU_MSG_RAM_START + offset;
278 break;
279 case 0:
280 ptr = priv->dmem + offset;
281 break;
282 default:
283 dev_emerg(priv->dev, "invalid message reply from DCPU: %#x\n",
284 response);
285 if (buf && size)
286 *size = sprintf(buf,
287 "FATAL: communication error with DCPU\n");
288 }
289
290 return ptr;
291}
292
Markus Mayer2f330ca2017-08-24 16:36:26 -0700293static int __send_command(struct private_data *priv, unsigned int cmd,
294 u32 result[])
295{
296 const u32 *msg = dpfe_commands[cmd];
297 void __iomem *regs = priv->regs;
298 unsigned int i, chksum;
299 int ret = 0;
300 u32 resp;
301
302 if (cmd >= DPFE_CMD_MAX)
303 return -1;
304
305 mutex_lock(&priv->lock);
306
Markus Mayera7c25752019-04-02 16:01:00 -0700307 /* Wait for DCPU to become ready */
308 for (i = 0; i < DELAY_LOOP_MAX; i++) {
309 resp = readl_relaxed(regs + REG_TO_HOST_MBOX);
310 if (resp == 0)
311 break;
312 msleep(1);
313 }
314 if (resp != 0) {
315 mutex_unlock(&priv->lock);
316 return -ETIMEDOUT;
317 }
318
Markus Mayer2f330ca2017-08-24 16:36:26 -0700319 /* Write command and arguments to message area */
320 for (i = 0; i < MSG_FIELD_MAX; i++)
321 writel_relaxed(msg[i], regs + DCPU_MSG_RAM(i));
322
323 /* Tell DCPU there is a command waiting */
324 writel_relaxed(1, regs + REG_TO_DCPU_MBOX);
325
326 /* Wait for DCPU to process the command */
327 for (i = 0; i < DELAY_LOOP_MAX; i++) {
328 /* Read response code */
329 resp = readl_relaxed(regs + REG_TO_HOST_MBOX);
330 if (resp > 0)
331 break;
Markus Mayer7ccd2ff2019-02-11 17:24:41 -0800332 msleep(1);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700333 }
334
335 if (i == DELAY_LOOP_MAX) {
336 resp = (DCPU_RET_ERR_TIMEDOUT & ~DCPU_RET_ERROR_BIT);
337 ret = -ffs(resp);
338 } else {
339 /* Read response data */
340 for (i = 0; i < MSG_FIELD_MAX; i++)
341 result[i] = readl_relaxed(regs + DCPU_MSG_RAM(i));
342 }
343
344 /* Tell DCPU we are done */
345 writel_relaxed(0, regs + REG_TO_HOST_MBOX);
346
347 mutex_unlock(&priv->lock);
348
349 if (ret)
350 return ret;
351
352 /* Verify response */
353 chksum = get_msg_chksum(result);
354 if (chksum != result[MSG_CHKSUM])
355 resp = DCPU_RET_ERR_CHKSUM;
356
357 if (resp != DCPU_RET_SUCCESS) {
358 resp &= ~DCPU_RET_ERROR_BIT;
359 ret = -ffs(resp);
360 }
361
362 return ret;
363}
364
365/* Ensure that the firmware file loaded meets all the requirements. */
366static int __verify_firmware(struct init_data *init,
367 const struct firmware *fw)
368{
369 const struct dpfe_firmware_header *header = (void *)fw->data;
370 unsigned int dmem_size, imem_size, total_size;
371 bool is_big_endian = false;
372 const u32 *chksum_ptr;
373
374 if (header->magic == DPFE_BE_MAGIC)
375 is_big_endian = true;
376 else if (header->magic != DPFE_LE_MAGIC)
377 return ERR_INVALID_MAGIC;
378
379 if (is_big_endian) {
380 dmem_size = be32_to_cpu(header->dmem_size);
381 imem_size = be32_to_cpu(header->imem_size);
382 } else {
383 dmem_size = le32_to_cpu(header->dmem_size);
384 imem_size = le32_to_cpu(header->imem_size);
385 }
386
387 /* Data and instruction sections are 32 bit words. */
388 if ((dmem_size % sizeof(u32)) != 0 || (imem_size % sizeof(u32)) != 0)
389 return ERR_INVALID_SIZE;
390
391 /*
392 * The header + the data section + the instruction section + the
393 * checksum must be equal to the total firmware size.
394 */
395 total_size = dmem_size + imem_size + sizeof(*header) +
396 sizeof(*chksum_ptr);
397 if (total_size != fw->size)
398 return ERR_INVALID_SIZE;
399
400 /* The checksum comes at the very end. */
401 chksum_ptr = (void *)fw->data + sizeof(*header) + dmem_size + imem_size;
402
403 init->is_big_endian = is_big_endian;
404 init->dmem_len = dmem_size;
405 init->imem_len = imem_size;
406 init->chksum = (is_big_endian)
407 ? be32_to_cpu(*chksum_ptr) : le32_to_cpu(*chksum_ptr);
408
409 return 0;
410}
411
412/* Verify checksum by reading back the firmware from co-processor RAM. */
413static int __verify_fw_checksum(struct init_data *init,
414 struct private_data *priv,
415 const struct dpfe_firmware_header *header,
416 u32 checksum)
417{
418 u32 magic, sequence, version, sum;
419 u32 __iomem *dmem = priv->dmem;
420 u32 __iomem *imem = priv->imem;
421 unsigned int i;
422
423 if (init->is_big_endian) {
424 magic = be32_to_cpu(header->magic);
425 sequence = be32_to_cpu(header->sequence);
426 version = be32_to_cpu(header->version);
427 } else {
428 magic = le32_to_cpu(header->magic);
429 sequence = le32_to_cpu(header->sequence);
430 version = le32_to_cpu(header->version);
431 }
432
433 sum = magic + sequence + version + init->dmem_len + init->imem_len;
434
435 for (i = 0; i < init->dmem_len / sizeof(u32); i++)
436 sum += readl_relaxed(dmem + i);
437
438 for (i = 0; i < init->imem_len / sizeof(u32); i++)
439 sum += readl_relaxed(imem + i);
440
441 return (sum == checksum) ? 0 : -1;
442}
443
444static int __write_firmware(u32 __iomem *mem, const u32 *fw,
445 unsigned int size, bool is_big_endian)
446{
447 unsigned int i;
448
449 /* Convert size to 32-bit words. */
450 size /= sizeof(u32);
451
452 /* It is recommended to clear the firmware area first. */
453 for (i = 0; i < size; i++)
454 writel_relaxed(0, mem + i);
455
456 /* Now copy it. */
457 if (is_big_endian) {
458 for (i = 0; i < size; i++)
459 writel_relaxed(be32_to_cpu(fw[i]), mem + i);
460 } else {
461 for (i = 0; i < size; i++)
462 writel_relaxed(le32_to_cpu(fw[i]), mem + i);
463 }
464
465 return 0;
466}
467
468static int brcmstb_dpfe_download_firmware(struct platform_device *pdev,
469 struct init_data *init)
470{
471 const struct dpfe_firmware_header *header;
472 unsigned int dmem_size, imem_size;
473 struct device *dev = &pdev->dev;
474 bool is_big_endian = false;
475 struct private_data *priv;
476 const struct firmware *fw;
477 const u32 *dmem, *imem;
478 const void *fw_blob;
479 int ret;
480
Markus Mayera56d3392017-10-02 16:13:47 -0700481 priv = platform_get_drvdata(pdev);
482
483 /*
484 * Skip downloading the firmware if the DCPU is already running and
485 * responding to commands.
486 */
487 if (is_dcpu_enabled(priv->regs)) {
488 u32 response[MSG_FIELD_MAX];
489
490 ret = __send_command(priv, DPFE_CMD_GET_INFO, response);
491 if (!ret)
492 return 0;
493 }
494
Markus Mayer2f330ca2017-08-24 16:36:26 -0700495 ret = request_firmware(&fw, FIRMWARE_NAME, dev);
496 /* request_firmware() prints its own error messages. */
497 if (ret)
498 return ret;
499
Markus Mayer2f330ca2017-08-24 16:36:26 -0700500 ret = __verify_firmware(init, fw);
501 if (ret)
502 return -EFAULT;
503
504 __disable_dcpu(priv->regs);
505
506 is_big_endian = init->is_big_endian;
507 dmem_size = init->dmem_len;
508 imem_size = init->imem_len;
509
510 /* At the beginning of the firmware blob is a header. */
511 header = (struct dpfe_firmware_header *)fw->data;
512 /* Void pointer to the beginning of the actual firmware. */
513 fw_blob = fw->data + sizeof(*header);
514 /* IMEM comes right after the header. */
515 imem = fw_blob;
516 /* DMEM follows after IMEM. */
517 dmem = fw_blob + imem_size;
518
519 ret = __write_firmware(priv->dmem, dmem, dmem_size, is_big_endian);
520 if (ret)
521 return ret;
522 ret = __write_firmware(priv->imem, imem, imem_size, is_big_endian);
523 if (ret)
524 return ret;
525
526 ret = __verify_fw_checksum(init, priv, header, init->chksum);
527 if (ret)
528 return ret;
529
530 __enable_dcpu(priv->regs);
531
532 return 0;
533}
534
535static ssize_t generic_show(unsigned int command, u32 response[],
Markus Mayer900c8f52019-02-11 17:24:42 -0800536 struct private_data *priv, char *buf)
Markus Mayer2f330ca2017-08-24 16:36:26 -0700537{
Markus Mayer2f330ca2017-08-24 16:36:26 -0700538 int ret;
539
Markus Mayer2f330ca2017-08-24 16:36:26 -0700540 if (!priv)
541 return sprintf(buf, "ERROR: driver private data not set\n");
542
543 ret = __send_command(priv, command, response);
544 if (ret < 0)
545 return sprintf(buf, "ERROR: %s\n", error_text[-ret]);
546
547 return 0;
548}
549
550static ssize_t show_info(struct device *dev, struct device_attribute *devattr,
551 char *buf)
552{
553 u32 response[MSG_FIELD_MAX];
Markus Mayer900c8f52019-02-11 17:24:42 -0800554 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700555 unsigned int info;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800556 ssize_t ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700557
Markus Mayer900c8f52019-02-11 17:24:42 -0800558 priv = dev_get_drvdata(dev);
559 ret = generic_show(DPFE_CMD_GET_INFO, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700560 if (ret)
561 return ret;
562
563 info = response[MSG_ARG0];
564
565 return sprintf(buf, "%u.%u.%u.%u\n",
566 (info >> 24) & 0xff,
567 (info >> 16) & 0xff,
568 (info >> 8) & 0xff,
569 info & 0xff);
570}
571
572static ssize_t show_refresh(struct device *dev,
573 struct device_attribute *devattr, char *buf)
574{
575 u32 response[MSG_FIELD_MAX];
576 void __iomem *info;
577 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700578 u8 refresh, sr_abort, ppre, thermal_offs, tuf;
579 u32 mr4;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800580 ssize_t ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700581
Markus Mayer900c8f52019-02-11 17:24:42 -0800582 priv = dev_get_drvdata(dev);
583 ret = generic_show(DPFE_CMD_GET_REFRESH, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700584 if (ret)
585 return ret;
586
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800587 info = get_msg_ptr(priv, response[MSG_ARG0], buf, &ret);
588 if (!info)
589 return ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700590
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800591 mr4 = (readl_relaxed(info + DRAM_INFO_MR4) >> DRAM_INFO_MR4_SHIFT) &
Markus Mayer1ffc0b52019-04-02 16:00:58 -0700592 DRAM_INFO_MR4_MASK;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700593
594 refresh = (mr4 >> DRAM_MR4_REFRESH) & DRAM_MR4_REFRESH_MASK;
595 sr_abort = (mr4 >> DRAM_MR4_SR_ABORT) & DRAM_MR4_SR_ABORT_MASK;
596 ppre = (mr4 >> DRAM_MR4_PPRE) & DRAM_MR4_PPRE_MASK;
597 thermal_offs = (mr4 >> DRAM_MR4_TH_OFFS) & DRAM_MR4_TH_OFFS_MASK;
598 tuf = (mr4 >> DRAM_MR4_TUF) & DRAM_MR4_TUF_MASK;
599
600 return sprintf(buf, "%#x %#x %#x %#x %#x %#x %#x\n",
601 readl_relaxed(info + DRAM_INFO_INTERVAL),
602 refresh, sr_abort, ppre, thermal_offs, tuf,
603 readl_relaxed(info + DRAM_INFO_ERROR));
604}
605
606static ssize_t store_refresh(struct device *dev, struct device_attribute *attr,
607 const char *buf, size_t count)
608{
609 u32 response[MSG_FIELD_MAX];
610 struct private_data *priv;
611 void __iomem *info;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700612 unsigned long val;
613 int ret;
614
615 if (kstrtoul(buf, 0, &val) < 0)
616 return -EINVAL;
617
618 priv = dev_get_drvdata(dev);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700619 ret = __send_command(priv, DPFE_CMD_GET_REFRESH, response);
620 if (ret)
621 return ret;
622
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800623 info = get_msg_ptr(priv, response[MSG_ARG0], NULL, NULL);
624 if (!info)
625 return -EIO;
626
Markus Mayer2f330ca2017-08-24 16:36:26 -0700627 writel_relaxed(val, info + DRAM_INFO_INTERVAL);
628
629 return count;
630}
631
632static ssize_t show_vendor(struct device *dev, struct device_attribute *devattr,
Markus Mayer1ffc0b52019-04-02 16:00:58 -0700633 char *buf)
Markus Mayer2f330ca2017-08-24 16:36:26 -0700634{
635 u32 response[MSG_FIELD_MAX];
636 struct private_data *priv;
637 void __iomem *info;
Markus Mayer9f2c4d92018-02-13 12:40:39 -0800638 ssize_t ret;
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800639 u32 mr5, mr6, mr7, mr8, err;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700640
Markus Mayer900c8f52019-02-11 17:24:42 -0800641 priv = dev_get_drvdata(dev);
642 ret = generic_show(DPFE_CMD_GET_VENDOR, response, priv, buf);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700643 if (ret)
644 return ret;
645
Markus Mayerfee5f1e2018-02-13 12:40:40 -0800646 info = get_msg_ptr(priv, response[MSG_ARG0], buf, &ret);
647 if (!info)
648 return ret;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700649
Markus Mayer78a6f5b2019-02-11 17:24:43 -0800650 mr5 = (readl_relaxed(info + DRAM_VENDOR_MR5) >> DRAM_VENDOR_SHIFT) &
651 DRAM_VENDOR_MASK;
652 mr6 = (readl_relaxed(info + DRAM_VENDOR_MR6) >> DRAM_VENDOR_SHIFT) &
653 DRAM_VENDOR_MASK;
654 mr7 = (readl_relaxed(info + DRAM_VENDOR_MR7) >> DRAM_VENDOR_SHIFT) &
655 DRAM_VENDOR_MASK;
656 mr8 = (readl_relaxed(info + DRAM_VENDOR_MR8) >> DRAM_VENDOR_SHIFT) &
657 DRAM_VENDOR_MASK;
658 err = readl_relaxed(info + DRAM_VENDOR_ERROR) & DRAM_VENDOR_MASK;
659
660 return sprintf(buf, "%#x %#x %#x %#x %#x\n", mr5, mr6, mr7, mr8, err);
Markus Mayer2f330ca2017-08-24 16:36:26 -0700661}
662
663static int brcmstb_dpfe_resume(struct platform_device *pdev)
664{
665 struct init_data init;
666
667 return brcmstb_dpfe_download_firmware(pdev, &init);
668}
669
670static DEVICE_ATTR(dpfe_info, 0444, show_info, NULL);
671static DEVICE_ATTR(dpfe_refresh, 0644, show_refresh, store_refresh);
672static DEVICE_ATTR(dpfe_vendor, 0444, show_vendor, NULL);
673static struct attribute *dpfe_attrs[] = {
674 &dev_attr_dpfe_info.attr,
675 &dev_attr_dpfe_refresh.attr,
676 &dev_attr_dpfe_vendor.attr,
677 NULL
678};
679ATTRIBUTE_GROUPS(dpfe);
680
681static int brcmstb_dpfe_probe(struct platform_device *pdev)
682{
683 struct device *dev = &pdev->dev;
684 struct private_data *priv;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700685 struct init_data init;
686 struct resource *res;
Markus Mayer2f330ca2017-08-24 16:36:26 -0700687 int ret;
688
689 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
690 if (!priv)
691 return -ENOMEM;
692
693 mutex_init(&priv->lock);
694 platform_set_drvdata(pdev, priv);
695
Markus Mayer2f330ca2017-08-24 16:36:26 -0700696 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-cpu");
697 priv->regs = devm_ioremap_resource(dev, res);
698 if (IS_ERR(priv->regs)) {
699 dev_err(dev, "couldn't map DCPU registers\n");
700 return -ENODEV;
701 }
702
703 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-dmem");
704 priv->dmem = devm_ioremap_resource(dev, res);
705 if (IS_ERR(priv->dmem)) {
706 dev_err(dev, "Couldn't map DCPU data memory\n");
707 return -ENOENT;
708 }
709
710 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dpfe-imem");
711 priv->imem = devm_ioremap_resource(dev, res);
712 if (IS_ERR(priv->imem)) {
713 dev_err(dev, "Couldn't map DCPU instruction memory\n");
714 return -ENOENT;
715 }
716
717 ret = brcmstb_dpfe_download_firmware(pdev, &init);
Markus Mayer6ca5d2ba2019-04-02 16:00:59 -0700718 if (ret) {
719 dev_err(dev, "Couldn't download firmware -- %d\n", ret);
Florian Fainellib1d09732018-03-27 16:40:38 -0700720 return ret;
Markus Mayer6ca5d2ba2019-04-02 16:00:59 -0700721 }
Markus Mayer2f330ca2017-08-24 16:36:26 -0700722
Florian Fainellib1d09732018-03-27 16:40:38 -0700723 ret = sysfs_create_groups(&pdev->dev.kobj, dpfe_groups);
724 if (!ret)
725 dev_info(dev, "registered.\n");
Markus Mayer2f330ca2017-08-24 16:36:26 -0700726
727 return ret;
728}
729
Florian Fainellib1d09732018-03-27 16:40:38 -0700730static int brcmstb_dpfe_remove(struct platform_device *pdev)
731{
732 sysfs_remove_groups(&pdev->dev.kobj, dpfe_groups);
733
734 return 0;
735}
736
Markus Mayer2f330ca2017-08-24 16:36:26 -0700737static const struct of_device_id brcmstb_dpfe_of_match[] = {
738 { .compatible = "brcm,dpfe-cpu", },
739 {}
740};
741MODULE_DEVICE_TABLE(of, brcmstb_dpfe_of_match);
742
743static struct platform_driver brcmstb_dpfe_driver = {
744 .driver = {
745 .name = DRVNAME,
746 .of_match_table = brcmstb_dpfe_of_match,
747 },
748 .probe = brcmstb_dpfe_probe,
Florian Fainellib1d09732018-03-27 16:40:38 -0700749 .remove = brcmstb_dpfe_remove,
Markus Mayer2f330ca2017-08-24 16:36:26 -0700750 .resume = brcmstb_dpfe_resume,
751};
752
753module_platform_driver(brcmstb_dpfe_driver);
754
755MODULE_AUTHOR("Markus Mayer <mmayer@broadcom.com>");
756MODULE_DESCRIPTION("BRCMSTB DDR PHY Front End Driver");
757MODULE_LICENSE("GPL");