blob: 7a5dfac4ab50054e0b991fb8cb7d4f7ea0e7548e [file] [log] [blame]
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +02001/*
2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
3 *
4 * Derived from linux/arch/mips/bcm63xx/cpu.c:
5 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
6 * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020013#include <errno.h>
14#include <ram.h>
15#include <asm/io.h>
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020016
Álvaro Fernández Rojas11471a62017-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 Rojascb936522018-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 Rojas11471a62017-05-16 18:39:02 +020028
Álvaro Fernández Rojasb6d6aad2017-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;
47 const struct bmips_ram_hw *hw;
48};
49
Álvaro Fernández Rojascb936522018-01-20 19:16:03 +010050static ulong bcm6318_get_ram_size(struct bmips_ram_priv *priv)
51{
52 u32 val;
53
54 val = readl_be(priv->regs + SDRAM_CFG_REG);
55 val = (val & SDRAM_6318_SPACE_MASK) >> SDRAM_6318_SPACE_SHIFT;
56
57 return (1 << (val + 20));
58}
59
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020060static ulong bcm6328_get_ram_size(struct bmips_ram_priv *priv)
61{
62 return readl_be(priv->regs + DDR_CSEND_REG) << 24;
63}
64
Álvaro Fernández Rojas029d7cf2017-05-16 18:39:01 +020065static ulong bmips_dram_size(unsigned int cols, unsigned int rows,
66 unsigned int is_32b, unsigned int banks)
67{
68 rows += 11; /* 0 => 11 address bits ... 2 => 13 address bits */
69 cols += 8; /* 0 => 8 address bits ... 2 => 10 address bits */
70 is_32b += 1;
71
72 return 1 << (cols + rows + is_32b + banks);
73}
74
Álvaro Fernández Rojas11471a62017-05-16 18:39:02 +020075static ulong bcm6338_get_ram_size(struct bmips_ram_priv *priv)
76{
77 unsigned int cols = 0, rows = 0, is_32b = 0, banks = 0;
78 u32 val;
79
80 val = readl_be(priv->regs + SDRAM_CFG_REG);
81 rows = (val & SDRAM_CFG_ROW_MASK) >> SDRAM_CFG_ROW_SHIFT;
82 cols = (val & SDRAM_CFG_COL_MASK) >> SDRAM_CFG_COL_SHIFT;
83 is_32b = (val & SDRAM_CFG_32B_MASK) ? 1 : 0;
84 banks = (val & SDRAM_CFG_BANK_MASK) ? 2 : 1;
85
86 return bmips_dram_size(cols, rows, is_32b, banks);
87}
88
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020089static ulong bcm6358_get_ram_size(struct bmips_ram_priv *priv)
90{
Álvaro Fernández Rojas029d7cf2017-05-16 18:39:01 +020091 unsigned int cols = 0, rows = 0, is_32b = 0;
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020092 u32 val;
93
94 val = readl_be(priv->regs + MEMC_CFG_REG);
95 rows = (val & MEMC_CFG_ROW_MASK) >> MEMC_CFG_ROW_SHIFT;
96 cols = (val & MEMC_CFG_COL_MASK) >> MEMC_CFG_COL_SHIFT;
Álvaro Fernández Rojas029d7cf2017-05-16 18:39:01 +020097 is_32b = (val & MEMC_CFG_32B_MASK) ? 0 : 1;
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +020098
Álvaro Fernández Rojas029d7cf2017-05-16 18:39:01 +020099 return bmips_dram_size(cols, rows, is_32b, 2);
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200100}
101
102static int bmips_ram_get_info(struct udevice *dev, struct ram_info *info)
103{
104 struct bmips_ram_priv *priv = dev_get_priv(dev);
105 const struct bmips_ram_hw *hw = priv->hw;
106
107 info->base = 0x80000000;
108 info->size = hw->get_ram_size(priv);
109
110 return 0;
111}
112
113static const struct ram_ops bmips_ram_ops = {
114 .get_info = bmips_ram_get_info,
115};
116
Álvaro Fernández Rojascb936522018-01-20 19:16:03 +0100117static const struct bmips_ram_hw bmips_ram_bcm6318 = {
118 .get_ram_size = bcm6318_get_ram_size,
119};
120
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200121static const struct bmips_ram_hw bmips_ram_bcm6328 = {
122 .get_ram_size = bcm6328_get_ram_size,
123};
124
Álvaro Fernández Rojas11471a62017-05-16 18:39:02 +0200125static const struct bmips_ram_hw bmips_ram_bcm6338 = {
126 .get_ram_size = bcm6338_get_ram_size,
127};
128
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200129static const struct bmips_ram_hw bmips_ram_bcm6358 = {
130 .get_ram_size = bcm6358_get_ram_size,
131};
132
133static const struct udevice_id bmips_ram_ids[] = {
134 {
Álvaro Fernández Rojascb936522018-01-20 19:16:03 +0100135 .compatible = "brcm,bcm6318-mc",
136 .data = (ulong)&bmips_ram_bcm6318,
137 }, {
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200138 .compatible = "brcm,bcm6328-mc",
139 .data = (ulong)&bmips_ram_bcm6328,
140 }, {
Álvaro Fernández Rojas11471a62017-05-16 18:39:02 +0200141 .compatible = "brcm,bcm6338-mc",
142 .data = (ulong)&bmips_ram_bcm6338,
143 }, {
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200144 .compatible = "brcm,bcm6358-mc",
145 .data = (ulong)&bmips_ram_bcm6358,
Álvaro Fernández Rojasd13179d2017-05-11 11:01:29 +0200146 }, { /* sentinel */ }
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200147};
148
149static int bmips_ram_probe(struct udevice *dev)
150{
151 struct bmips_ram_priv *priv = dev_get_priv(dev);
152 const struct bmips_ram_hw *hw =
153 (const struct bmips_ram_hw *)dev_get_driver_data(dev);
154 fdt_addr_t addr;
155 fdt_size_t size;
156
Simon Glassba1dea42017-05-17 17:18:05 -0600157 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasb6d6aad2017-04-25 00:39:19 +0200158 if (addr == FDT_ADDR_T_NONE)
159 return -EINVAL;
160
161 priv->regs = ioremap(addr, size);
162 priv->hw = hw;
163
164 return 0;
165}
166
167U_BOOT_DRIVER(bmips_ram) = {
168 .name = "bmips-mc",
169 .id = UCLASS_RAM,
170 .of_match = bmips_ram_ids,
171 .probe = bmips_ram_probe,
172 .priv_auto_alloc_size = sizeof(struct bmips_ram_priv),
173 .ops = &bmips_ram_ops,
174 .flags = DM_FLAG_PRE_RELOC,
175};