memconf.c 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2011-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/sizes.h>
11 12 13

#include "../init.h"
#include "../sg-regs.h"
14 15 16 17 18 19 20 21 22 23

int memconf_init(const struct uniphier_board_data *bd)
{
	u32 tmp = 0;
	unsigned long size_per_word;

	tmp = readl(SG_MEMCONF);

	tmp &= ~(SG_MEMCONF_CH0_SZ_MASK | SG_MEMCONF_CH0_NUM_MASK);

24
	switch (bd->dram_ch[0].width) {
25 26
	case 16:
		tmp |= SG_MEMCONF_CH0_NUM_1;
27
		size_per_word = bd->dram_ch[0].size;
28 29 30
		break;
	case 32:
		tmp |= SG_MEMCONF_CH0_NUM_2;
31
		size_per_word = bd->dram_ch[0].size >> 1;
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
		break;
	default:
		pr_err("error: unsupported DRAM Ch0 width\n");
		return -EINVAL;
	}

	/* Set DDR size */
	switch (size_per_word) {
	case SZ_64M:
		tmp |= SG_MEMCONF_CH0_SZ_64M;
		break;
	case SZ_128M:
		tmp |= SG_MEMCONF_CH0_SZ_128M;
		break;
	case SZ_256M:
		tmp |= SG_MEMCONF_CH0_SZ_256M;
		break;
	case SZ_512M:
		tmp |= SG_MEMCONF_CH0_SZ_512M;
		break;
	case SZ_1G:
		tmp |= SG_MEMCONF_CH0_SZ_1G;
		break;
	default:
		pr_err("error: unsupported DRAM Ch0 size\n");
		return -EINVAL;
	}

	tmp &= ~(SG_MEMCONF_CH1_SZ_MASK | SG_MEMCONF_CH1_NUM_MASK);

62
	switch (bd->dram_ch[1].width) {
63 64
	case 16:
		tmp |= SG_MEMCONF_CH1_NUM_1;
65
		size_per_word = bd->dram_ch[1].size;
66 67 68
		break;
	case 32:
		tmp |= SG_MEMCONF_CH1_NUM_2;
69
		size_per_word = bd->dram_ch[1].size >> 1;
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
		break;
	default:
		pr_err("error: unsupported DRAM Ch1 width\n");
		return -EINVAL;
	}

	switch (size_per_word) {
	case SZ_64M:
		tmp |= SG_MEMCONF_CH1_SZ_64M;
		break;
	case SZ_128M:
		tmp |= SG_MEMCONF_CH1_SZ_128M;
		break;
	case SZ_256M:
		tmp |= SG_MEMCONF_CH1_SZ_256M;
		break;
	case SZ_512M:
		tmp |= SG_MEMCONF_CH1_SZ_512M;
		break;
	case SZ_1G:
		tmp |= SG_MEMCONF_CH1_SZ_1G;
		break;
	default:
		pr_err("error: unsupported DRAM Ch1 size\n");
		return -EINVAL;
	}

97
	if (bd->dram_ch[0].base + bd->dram_ch[0].size < bd->dram_ch[1].base)
98 99 100 101 102 103 104 105
		tmp |= SG_MEMCONF_SPARSEMEM;
	else
		tmp &= ~SG_MEMCONF_SPARSEMEM;

	writel(tmp, SG_MEMCONF);

	return 0;
}