1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19
20
21
22
23
24
25
26
27
28
29
30
31
32struct da8xx_ddrctl_config_knob {
33 const char *name;
34 u32 reg;
35 u32 mask;
36 u32 shift;
37};
38
39static const struct da8xx_ddrctl_config_knob da8xx_ddrctl_knobs[] = {
40 {
41 .name = "da850-pbbpr",
42 .reg = 0x20,
43 .mask = 0xffffff00,
44 .shift = 0,
45 },
46};
47
48struct da8xx_ddrctl_setting {
49 const char *name;
50 u32 val;
51};
52
53struct da8xx_ddrctl_board_settings {
54 const char *board;
55 const struct da8xx_ddrctl_setting *settings;
56};
57
58static const struct da8xx_ddrctl_setting da850_lcdk_ddrctl_settings[] = {
59 {
60 .name = "da850-pbbpr",
61 .val = 0x20,
62 },
63 { }
64};
65
66static const struct da8xx_ddrctl_board_settings da8xx_ddrctl_board_confs[] = {
67 {
68 .board = "ti,da850-lcdk",
69 .settings = da850_lcdk_ddrctl_settings,
70 },
71};
72
73static const struct da8xx_ddrctl_config_knob *
74da8xx_ddrctl_match_knob(const struct da8xx_ddrctl_setting *setting)
75{
76 const struct da8xx_ddrctl_config_knob *knob;
77 int i;
78
79 for (i = 0; i < ARRAY_SIZE(da8xx_ddrctl_knobs); i++) {
80 knob = &da8xx_ddrctl_knobs[i];
81
82 if (strcmp(knob->name, setting->name) == 0)
83 return knob;
84 }
85
86 return NULL;
87}
88
89static const struct da8xx_ddrctl_setting *da8xx_ddrctl_get_board_settings(void)
90{
91 const struct da8xx_ddrctl_board_settings *board_settings;
92 int i;
93
94 for (i = 0; i < ARRAY_SIZE(da8xx_ddrctl_board_confs); i++) {
95 board_settings = &da8xx_ddrctl_board_confs[i];
96
97 if (of_machine_is_compatible(board_settings->board))
98 return board_settings->settings;
99 }
100
101 return NULL;
102}
103
104static int da8xx_ddrctl_probe(struct platform_device *pdev)
105{
106 const struct da8xx_ddrctl_config_knob *knob;
107 const struct da8xx_ddrctl_setting *setting;
108 struct device_node *node;
109 struct resource *res;
110 void __iomem *ddrctl;
111 struct device *dev;
112 u32 reg;
113
114 dev = &pdev->dev;
115 node = dev->of_node;
116
117 setting = da8xx_ddrctl_get_board_settings();
118 if (!setting) {
119 dev_err(dev, "no settings defined for this board\n");
120 return -EINVAL;
121 }
122
123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
124 ddrctl = devm_ioremap_resource(dev, res);
125 if (IS_ERR(ddrctl)) {
126 dev_err(dev, "unable to map memory controller registers\n");
127 return PTR_ERR(ddrctl);
128 }
129
130 for (; setting->name; setting++) {
131 knob = da8xx_ddrctl_match_knob(setting);
132 if (!knob) {
133 dev_warn(dev,
134 "no such config option: %s\n", setting->name);
135 continue;
136 }
137
138 if (knob->reg + sizeof(u32) > resource_size(res)) {
139 dev_warn(dev,
140 "register offset of '%s' exceeds mapped memory size\n",
141 knob->name);
142 continue;
143 }
144
145 reg = readl(ddrctl + knob->reg);
146 reg &= knob->mask;
147 reg |= setting->val << knob->shift;
148
149 dev_dbg(dev, "writing 0x%08x to %s\n", reg, setting->name);
150
151 writel(reg, ddrctl + knob->reg);
152 }
153
154 return 0;
155}
156
157static const struct of_device_id da8xx_ddrctl_of_match[] = {
158 { .compatible = "ti,da850-ddr-controller", },
159 { },
160};
161
162static struct platform_driver da8xx_ddrctl_driver = {
163 .probe = da8xx_ddrctl_probe,
164 .driver = {
165 .name = "da850-ddr-controller",
166 .of_match_table = da8xx_ddrctl_of_match,
167 },
168};
169module_platform_driver(da8xx_ddrctl_driver);
170
171MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
172MODULE_DESCRIPTION("TI da8xx DDR2/mDDR controller driver");
173MODULE_LICENSE("GPL v2");
174