da8xx-ddrctl.c 4.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*
 * TI da8xx DDR2/mDDR controller driver
 *
 * Copyright (C) 2016 BayLibre SAS
 *
 * Author:
 *   Bartosz Golaszewski <bgolaszewski@baylibre.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/io.h>

/*
 * REVISIT: Linux doesn't have a good framework for the kind of performance
 * knobs this driver controls. We can't use device tree properties as it deals
 * with hardware configuration rather than description. We also don't want to
 * commit to maintaining some random sysfs attributes.
 *
 * For now we just hardcode the register values for the boards that need
 * some changes (as is the case for the LCD controller on da850-lcdk - the
 * first board we support here). When linux gets an appropriate framework,
 * we'll easily convert the driver to it.
 */

struct da8xx_ddrctl_config_knob {
	const char *name;
	u32 reg;
	u32 mask;
	u32 shift;
};

static const struct da8xx_ddrctl_config_knob da8xx_ddrctl_knobs[] = {
	{
		.name = "da850-pbbpr",
		.reg = 0x20,
		.mask = 0xffffff00,
		.shift = 0,
	},
};

struct da8xx_ddrctl_setting {
	const char *name;
	u32 val;
};

struct da8xx_ddrctl_board_settings {
	const char *board;
	const struct da8xx_ddrctl_setting *settings;
};

static const struct da8xx_ddrctl_setting da850_lcdk_ddrctl_settings[] = {
	{
		.name = "da850-pbbpr",
		.val = 0x20,
	},
	{ }
};

static const struct da8xx_ddrctl_board_settings da8xx_ddrctl_board_confs[] = {
	{
		.board = "ti,da850-lcdk",
		.settings = da850_lcdk_ddrctl_settings,
	},
};

static const struct da8xx_ddrctl_config_knob *
da8xx_ddrctl_match_knob(const struct da8xx_ddrctl_setting *setting)
{
	const struct da8xx_ddrctl_config_knob *knob;
	int i;

	for (i = 0; i < ARRAY_SIZE(da8xx_ddrctl_knobs); i++) {
		knob = &da8xx_ddrctl_knobs[i];

		if (strcmp(knob->name, setting->name) == 0)
			return knob;
	}

	return NULL;
}

static const struct da8xx_ddrctl_setting *da8xx_ddrctl_get_board_settings(void)
{
	const struct da8xx_ddrctl_board_settings *board_settings;
	int i;

	for (i = 0; i < ARRAY_SIZE(da8xx_ddrctl_board_confs); i++) {
		board_settings = &da8xx_ddrctl_board_confs[i];

		if (of_machine_is_compatible(board_settings->board))
			return board_settings->settings;
	}

	return NULL;
}

static int da8xx_ddrctl_probe(struct platform_device *pdev)
{
	const struct da8xx_ddrctl_config_knob *knob;
	const struct da8xx_ddrctl_setting *setting;
	struct device_node *node;
	struct resource *res;
	void __iomem *ddrctl;
	struct device *dev;
	u32 reg;

	dev = &pdev->dev;
	node = dev->of_node;

	setting = da8xx_ddrctl_get_board_settings();
	if (!setting) {
119
		dev_err(dev, "no settings defined for this board\n");
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		return -EINVAL;
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	ddrctl = devm_ioremap_resource(dev, res);
	if (IS_ERR(ddrctl)) {
		dev_err(dev, "unable to map memory controller registers\n");
		return PTR_ERR(ddrctl);
	}

	for (; setting->name; setting++) {
		knob = da8xx_ddrctl_match_knob(setting);
		if (!knob) {
			dev_warn(dev,
				 "no such config option: %s\n", setting->name);
			continue;
		}

		if (knob->reg + sizeof(u32) > resource_size(res)) {
			dev_warn(dev,
				 "register offset of '%s' exceeds mapped memory size\n",
				 knob->name);
			continue;
		}

		reg = readl(ddrctl + knob->reg);
		reg &= knob->mask;
		reg |= setting->val << knob->shift;

		dev_dbg(dev, "writing 0x%08x to %s\n", reg, setting->name);

		writel(reg, ddrctl + knob->reg);
	}

	return 0;
}

static const struct of_device_id da8xx_ddrctl_of_match[] = {
	{ .compatible = "ti,da850-ddr-controller", },
	{ },
};

static struct platform_driver da8xx_ddrctl_driver = {
	.probe = da8xx_ddrctl_probe,
	.driver = {
		.name = "da850-ddr-controller",
		.of_match_table = da8xx_ddrctl_of_match,
	},
};
module_platform_driver(da8xx_ddrctl_driver);

MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
MODULE_DESCRIPTION("TI da8xx DDR2/mDDR controller driver");
MODULE_LICENSE("GPL v2");