blob: 8f953e79afd791a55a4f02a26184dbbda6678687 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 *
5 * Derived from linux/arch/mips/bcm63xx/cpu.c:
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +02008 */
9
10#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020012#include <errno.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070013#include <init.h>
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020014#include <ram.h>
15#include <asm/io.h>
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020016
Álvaro Fernández Rojas5a0efcf2017-05-16 18:39:02 +020017#define SDRAM_CFG_REG 0x0
18#define SDRAM_CFG_COL_SHIFT 4
19#define SDRAM_CFG_COL_MASK (0x3 << SDRAM_CFG_COL_SHIFT)
20#define SDRAM_CFG_ROW_SHIFT 6
21#define SDRAM_CFG_ROW_MASK (0x3 << SDRAM_CFG_ROW_SHIFT)
22#define SDRAM_CFG_32B_SHIFT 10
23#define SDRAM_CFG_32B_MASK (1 << SDRAM_CFG_32B_SHIFT)
24#define SDRAM_CFG_BANK_SHIFT 13
25#define SDRAM_CFG_BANK_MASK (1 << SDRAM_CFG_BANK_SHIFT)
Álvaro Fernández Rojasa80bf5e2018-01-20 19:16:03 +010026#define SDRAM_6318_SPACE_SHIFT 4
27#define SDRAM_6318_SPACE_MASK (0xf << SDRAM_6318_SPACE_SHIFT)
Álvaro Fernández Rojas5a0efcf2017-05-16 18:39:02 +020028
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020029#define MEMC_CFG_REG 0x4
30#define MEMC_CFG_32B_SHIFT 1
31#define MEMC_CFG_32B_MASK (1 << MEMC_CFG_32B_SHIFT)
32#define MEMC_CFG_COL_SHIFT 3
33#define MEMC_CFG_COL_MASK (0x3 << MEMC_CFG_COL_SHIFT)
34#define MEMC_CFG_ROW_SHIFT 6
35#define MEMC_CFG_ROW_MASK (0x3 << MEMC_CFG_ROW_SHIFT)
36
37#define DDR_CSEND_REG 0x8
38
39struct bmips_ram_priv;
40
41struct bmips_ram_hw {
42 ulong (*get_ram_size)(struct bmips_ram_priv *);
43};
44
45struct bmips_ram_priv {
46 void __iomem *regs;
Philippe Reynes3e4a68d2018-07-16 19:06:13 +020047 u32 force_size;
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020048 const struct bmips_ram_hw *hw;
49};
50
Álvaro Fernández Rojasa80bf5e2018-01-20 19:16:03 +010051static ulong bcm6318_get_ram_size(struct bmips_ram_priv *priv)
52{
53 u32 val;
54
55 val = readl_be(priv->regs + SDRAM_CFG_REG);
56 val = (val & SDRAM_6318_SPACE_MASK) >> SDRAM_6318_SPACE_SHIFT;
57
58 return (1 << (val + 20));
59}
60
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020061static ulong bcm6328_get_ram_size(struct bmips_ram_priv *priv)
62{
63 return readl_be(priv->regs + DDR_CSEND_REG) << 24;
64}
65
Álvaro Fernández Rojas21659612017-05-16 18:39:01 +020066static ulong bmips_dram_size(unsigned int cols, unsigned int rows,
67 unsigned int is_32b, unsigned int banks)
68{
69 rows += 11; /* 0 => 11 address bits ... 2 => 13 address bits */
70 cols += 8; /* 0 => 8 address bits ... 2 => 10 address bits */
71 is_32b += 1;
72
73 return 1 << (cols + rows + is_32b + banks);
74}
75
Álvaro Fernández Rojas5a0efcf2017-05-16 18:39:02 +020076static ulong bcm6338_get_ram_size(struct bmips_ram_priv *priv)
77{
78 unsigned int cols = 0, rows = 0, is_32b = 0, banks = 0;
79 u32 val;
80
81 val = readl_be(priv->regs + SDRAM_CFG_REG);
82 rows = (val & SDRAM_CFG_ROW_MASK) >> SDRAM_CFG_ROW_SHIFT;
83 cols = (val & SDRAM_CFG_COL_MASK) >> SDRAM_CFG_COL_SHIFT;
84 is_32b = (val & SDRAM_CFG_32B_MASK) ? 1 : 0;
85 banks = (val & SDRAM_CFG_BANK_MASK) ? 2 : 1;
86
87 return bmips_dram_size(cols, rows, is_32b, banks);
88}
89
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020090static ulong bcm6358_get_ram_size(struct bmips_ram_priv *priv)
91{
Álvaro Fernández Rojas21659612017-05-16 18:39:01 +020092 unsigned int cols = 0, rows = 0, is_32b = 0;
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020093 u32 val;
94
95 val = readl_be(priv->regs + MEMC_CFG_REG);
96 rows = (val & MEMC_CFG_ROW_MASK) >> MEMC_CFG_ROW_SHIFT;
97 cols = (val & MEMC_CFG_COL_MASK) >> MEMC_CFG_COL_SHIFT;
Álvaro Fernández Rojas21659612017-05-16 18:39:01 +020098 is_32b = (val & MEMC_CFG_32B_MASK) ? 0 : 1;
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +020099
Álvaro Fernández Rojas21659612017-05-16 18:39:01 +0200100 return bmips_dram_size(cols, rows, is_32b, 2);
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200101}
102
103static int bmips_ram_get_info(struct udevice *dev, struct ram_info *info)
104{
105 struct bmips_ram_priv *priv = dev_get_priv(dev);
106 const struct bmips_ram_hw *hw = priv->hw;
107
108 info->base = 0x80000000;
Philippe Reynes3e4a68d2018-07-16 19:06:13 +0200109 if (priv->force_size)
110 info->size = priv->force_size;
111 else
112 info->size = hw->get_ram_size(priv);
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200113
114 return 0;
115}
116
117static const struct ram_ops bmips_ram_ops = {
118 .get_info = bmips_ram_get_info,
119};
120
Álvaro Fernández Rojasa80bf5e2018-01-20 19:16:03 +0100121static const struct bmips_ram_hw bmips_ram_bcm6318 = {
122 .get_ram_size = bcm6318_get_ram_size,
123};
124
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200125static const struct bmips_ram_hw bmips_ram_bcm6328 = {
126 .get_ram_size = bcm6328_get_ram_size,
127};
128
Álvaro Fernández Rojas5a0efcf2017-05-16 18:39:02 +0200129static const struct bmips_ram_hw bmips_ram_bcm6338 = {
130 .get_ram_size = bcm6338_get_ram_size,
131};
132
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200133static const struct bmips_ram_hw bmips_ram_bcm6358 = {
134 .get_ram_size = bcm6358_get_ram_size,
135};
136
137static const struct udevice_id bmips_ram_ids[] = {
138 {
Álvaro Fernández Rojasa80bf5e2018-01-20 19:16:03 +0100139 .compatible = "brcm,bcm6318-mc",
140 .data = (ulong)&bmips_ram_bcm6318,
141 }, {
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200142 .compatible = "brcm,bcm6328-mc",
143 .data = (ulong)&bmips_ram_bcm6328,
144 }, {
Álvaro Fernández Rojas5a0efcf2017-05-16 18:39:02 +0200145 .compatible = "brcm,bcm6338-mc",
146 .data = (ulong)&bmips_ram_bcm6338,
147 }, {
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200148 .compatible = "brcm,bcm6358-mc",
149 .data = (ulong)&bmips_ram_bcm6358,
Álvaro Fernández Rojasb493a352017-05-11 11:01:29 +0200150 }, { /* sentinel */ }
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200151};
152
153static int bmips_ram_probe(struct udevice *dev)
154{
155 struct bmips_ram_priv *priv = dev_get_priv(dev);
156 const struct bmips_ram_hw *hw =
157 (const struct bmips_ram_hw *)dev_get_driver_data(dev);
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200158
Álvaro Fernández Rojas13a7bfe2018-03-22 19:39:38 +0100159 priv->regs = dev_remap_addr(dev);
160 if (!priv->regs)
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200161 return -EINVAL;
162
Philippe Reynes3e4a68d2018-07-16 19:06:13 +0200163 dev_read_u32(dev, "force-size", &priv->force_size);
164
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200165 priv->hw = hw;
166
167 return 0;
168}
169
170U_BOOT_DRIVER(bmips_ram) = {
171 .name = "bmips-mc",
172 .id = UCLASS_RAM,
173 .of_match = bmips_ram_ids,
174 .probe = bmips_ram_probe,
175 .priv_auto_alloc_size = sizeof(struct bmips_ram_priv),
176 .ops = &bmips_ram_ops,
Álvaro Fernández Rojas193030e2017-04-25 00:39:19 +0200177};