1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
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>
17 #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)
26 #define SDRAM_6318_SPACE_SHIFT 4
27 #define SDRAM_6318_SPACE_MASK (0xf << SDRAM_6318_SPACE_SHIFT)
29 #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)
37 #define DDR_CSEND_REG 0x8
39 struct bmips_ram_priv;
42 ulong (*get_ram_size)(struct bmips_ram_priv *);
45 struct bmips_ram_priv {
48 const struct bmips_ram_hw *hw;
51 static ulong bcm6318_get_ram_size(struct bmips_ram_priv *priv)
55 val = readl_be(priv->regs + SDRAM_CFG_REG);
56 val = (val & SDRAM_6318_SPACE_MASK) >> SDRAM_6318_SPACE_SHIFT;
58 return (1 << (val + 20));
61 static ulong bcm6328_get_ram_size(struct bmips_ram_priv *priv)
63 return readl_be(priv->regs + DDR_CSEND_REG) << 24;
66 static ulong bmips_dram_size(unsigned int cols, unsigned int rows,
67 unsigned int is_32b, unsigned int banks)
69 rows += 11; /* 0 => 11 address bits ... 2 => 13 address bits */
70 cols += 8; /* 0 => 8 address bits ... 2 => 10 address bits */
73 return 1 << (cols + rows + is_32b + banks);
76 static ulong bcm6338_get_ram_size(struct bmips_ram_priv *priv)
78 unsigned int cols = 0, rows = 0, is_32b = 0, banks = 0;
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;
87 return bmips_dram_size(cols, rows, is_32b, banks);
90 static ulong bcm6358_get_ram_size(struct bmips_ram_priv *priv)
92 unsigned int cols = 0, rows = 0, is_32b = 0;
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;
98 is_32b = (val & MEMC_CFG_32B_MASK) ? 0 : 1;
100 return bmips_dram_size(cols, rows, is_32b, 2);
103 static int bmips_ram_get_info(struct udevice *dev, struct ram_info *info)
105 struct bmips_ram_priv *priv = dev_get_priv(dev);
106 const struct bmips_ram_hw *hw = priv->hw;
108 info->base = 0x80000000;
109 if (priv->force_size)
110 info->size = priv->force_size;
112 info->size = hw->get_ram_size(priv);
117 static const struct ram_ops bmips_ram_ops = {
118 .get_info = bmips_ram_get_info,
121 static const struct bmips_ram_hw bmips_ram_bcm6318 = {
122 .get_ram_size = bcm6318_get_ram_size,
125 static const struct bmips_ram_hw bmips_ram_bcm6328 = {
126 .get_ram_size = bcm6328_get_ram_size,
129 static const struct bmips_ram_hw bmips_ram_bcm6338 = {
130 .get_ram_size = bcm6338_get_ram_size,
133 static const struct bmips_ram_hw bmips_ram_bcm6358 = {
134 .get_ram_size = bcm6358_get_ram_size,
137 static const struct udevice_id bmips_ram_ids[] = {
139 .compatible = "brcm,bcm6318-mc",
140 .data = (ulong)&bmips_ram_bcm6318,
142 .compatible = "brcm,bcm6328-mc",
143 .data = (ulong)&bmips_ram_bcm6328,
145 .compatible = "brcm,bcm6338-mc",
146 .data = (ulong)&bmips_ram_bcm6338,
148 .compatible = "brcm,bcm6358-mc",
149 .data = (ulong)&bmips_ram_bcm6358,
150 }, { /* sentinel */ }
153 static int bmips_ram_probe(struct udevice *dev)
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);
159 priv->regs = dev_remap_addr(dev);
163 dev_read_u32(dev, "force-size", &priv->force_size);
170 U_BOOT_DRIVER(bmips_ram) = {
173 .of_match = bmips_ram_ids,
174 .probe = bmips_ram_probe,
175 .priv_auto = sizeof(struct bmips_ram_priv),
176 .ops = &bmips_ram_ops,