]> git.sur5r.net Git - u-boot/blob - drivers/ram/bmips_ram.c
dm: ram: bmips: split bcm6358_get_ram_size
[u-boot] / drivers / ram / bmips_ram.c
1 /*
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>
12 #include <errno.h>
13 #include <ram.h>
14 #include <asm/io.h>
15 #include <dm/device.h>
16
17 #define MEMC_CFG_REG            0x4
18 #define MEMC_CFG_32B_SHIFT      1
19 #define MEMC_CFG_32B_MASK       (1 << MEMC_CFG_32B_SHIFT)
20 #define MEMC_CFG_COL_SHIFT      3
21 #define MEMC_CFG_COL_MASK       (0x3 << MEMC_CFG_COL_SHIFT)
22 #define MEMC_CFG_ROW_SHIFT      6
23 #define MEMC_CFG_ROW_MASK       (0x3 << MEMC_CFG_ROW_SHIFT)
24
25 #define DDR_CSEND_REG           0x8
26
27 struct bmips_ram_priv;
28
29 struct bmips_ram_hw {
30         ulong (*get_ram_size)(struct bmips_ram_priv *);
31 };
32
33 struct bmips_ram_priv {
34         void __iomem *regs;
35         const struct bmips_ram_hw *hw;
36 };
37
38 static ulong bcm6328_get_ram_size(struct bmips_ram_priv *priv)
39 {
40         return readl_be(priv->regs + DDR_CSEND_REG) << 24;
41 }
42
43 static ulong bmips_dram_size(unsigned int cols, unsigned int rows,
44                              unsigned int is_32b, unsigned int banks)
45 {
46         rows += 11; /* 0 => 11 address bits ... 2 => 13 address bits */
47         cols += 8; /* 0 => 8 address bits ... 2 => 10 address bits */
48         is_32b += 1;
49
50         return 1 << (cols + rows + is_32b + banks);
51 }
52
53 static ulong bcm6358_get_ram_size(struct bmips_ram_priv *priv)
54 {
55         unsigned int cols = 0, rows = 0, is_32b = 0;
56         u32 val;
57
58         val = readl_be(priv->regs + MEMC_CFG_REG);
59         rows = (val & MEMC_CFG_ROW_MASK) >> MEMC_CFG_ROW_SHIFT;
60         cols = (val & MEMC_CFG_COL_MASK) >> MEMC_CFG_COL_SHIFT;
61         is_32b = (val & MEMC_CFG_32B_MASK) ? 0 : 1;
62
63         return bmips_dram_size(cols, rows, is_32b, 2);
64 }
65
66 static int bmips_ram_get_info(struct udevice *dev, struct ram_info *info)
67 {
68         struct bmips_ram_priv *priv = dev_get_priv(dev);
69         const struct bmips_ram_hw *hw = priv->hw;
70
71         info->base = 0x80000000;
72         info->size = hw->get_ram_size(priv);
73
74         return 0;
75 }
76
77 static const struct ram_ops bmips_ram_ops = {
78         .get_info = bmips_ram_get_info,
79 };
80
81 static const struct bmips_ram_hw bmips_ram_bcm6328 = {
82         .get_ram_size = bcm6328_get_ram_size,
83 };
84
85 static const struct bmips_ram_hw bmips_ram_bcm6358 = {
86         .get_ram_size = bcm6358_get_ram_size,
87 };
88
89 static const struct udevice_id bmips_ram_ids[] = {
90         {
91                 .compatible = "brcm,bcm6328-mc",
92                 .data = (ulong)&bmips_ram_bcm6328,
93         }, {
94                 .compatible = "brcm,bcm6358-mc",
95                 .data = (ulong)&bmips_ram_bcm6358,
96         }, { /* sentinel */ }
97 };
98
99 static int bmips_ram_probe(struct udevice *dev)
100 {
101         struct bmips_ram_priv *priv = dev_get_priv(dev);
102         const struct bmips_ram_hw *hw =
103                 (const struct bmips_ram_hw *)dev_get_driver_data(dev);
104         fdt_addr_t addr;
105         fdt_size_t size;
106
107         addr = dev_get_addr_size_index(dev, 0, &size);
108         if (addr == FDT_ADDR_T_NONE)
109                 return -EINVAL;
110
111         priv->regs = ioremap(addr, size);
112         priv->hw = hw;
113
114         return 0;
115 }
116
117 U_BOOT_DRIVER(bmips_ram) = {
118         .name = "bmips-mc",
119         .id = UCLASS_RAM,
120         .of_match = bmips_ram_ids,
121         .probe = bmips_ram_probe,
122         .priv_auto_alloc_size = sizeof(struct bmips_ram_priv),
123         .ops = &bmips_ram_ops,
124         .flags = DM_FLAG_PRE_RELOC,
125 };