summaryrefstree
path: root/kernel/drivers/media/tdmb/tdmb_ebi.c
blob: 989bafc504689e9063d5437f234e0808fb6a9cee (plain)
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
119
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
*
* drivers/media/tdmb/tdmb_ebi.c
*
* tdmb driver
*
* Copyright (C) (2011, Samsung Electronics)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

#include <linux/io.h>
#include <linux/of_gpio.h>
#include <linux/clk.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <plat/gpio-cfg.h>
#include <plat/regs-srom.h>
#include <mach/gpio.h>

#include "tdmb.h"

static struct tdmb_ebi_dt_data *ebi_dt_pdata;
static void __iomem *v_addr_ebi_cs_base;


/* SROMC bank attributes in BW (Bus width and Wait control) register */
enum sromc_bank_attr {
	SROMC_DATA_16   = 0x1,	/* 16-bit data bus	*/
	SROMC_BYTE_ADDR = 0x2,	/* Byte base address	*/
	SROMC_WAIT_EN   = 0x4,	/* Wait enabled		*/
	SROMC_BYTE_EN   = 0x8,	/* Byte access enabled	*/
	SROMC_ATTR_MASK = 0xF
};

/* SROMC bank configuration */
struct sromc_bank_cfg {
	unsigned csn;		/* CSn #			*/
	unsigned attr;		/* SROMC bank attributes	*/
	unsigned size;		/* Size of a memory		*/
	unsigned addr;		/* Start address (physical)	*/
};

/* SROMC bank access timing configuration */
struct sromc_timing_cfg {
	u32 tacs;		/* Address set-up before CSn		*/
	u32 tcos;		/* Chip selection set-up before OEn	*/
	u32 tacc;		/* Access cycle				*/
	u32 tcoh;		/* Chip selection hold on OEn		*/
	u32 tcah;		/* Address holding time after CSn	*/
	u32 tacp;		/* Page mode access cycle at Page mode	*/
	u32 pmc;		/* Page Mode config			*/
};

/**
 * sromc_enable
 *
 * Enables SROM controller (SROMC) block
 *
 */
static int sromc_enable(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct clk *clk;

	clk = devm_clk_get(dev, "sromc_clk");
	if (IS_ERR(clk)) {
		DPRINTK("Failed to get tdmb_sromc clock\n");
		return -1;
	}

	if (clk_prepare_enable(clk)) {
		DPRINTK("%s: clk_prepare_enable failed\n", __func__);
		return -1;
	}
	return 0;
}

/**
 * sromc_config_demux_gpio
 *
 * Configures GPIO pins for REn, WEn, LBn, UBn, address bus, and data bus
 * as demux mode
 *
 * Returns 0 if there is no error
 *
 */
static int sromc_config_demux_gpio(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct pinctrl *pinctrl;

	pinctrl = devm_pinctrl_get_select_default(dev);
	if (IS_ERR(pinctrl))
		DPRINTK("%s: Failed to configure pinctrl\n", __func__);

	return 0;
}

/**
 * sromc_config_access_attr
 * @csn: CSn number
 * @attr: SROMC attribute for this CSn
 *
 * Configures SROMC attribute for a CSn
 *
 */
static void sromc_config_access_attr(unsigned int csn, unsigned int attr)
{
	unsigned int bw = 0;	/* Bus width and Wait control */

	DPRINTK("%s: for CSn%d\n", __func__, csn);

	bw = __raw_readl(S5P_SROM_BW);
	DPRINTK("%s: old BW setting = 0x%08X\n", __func__, bw);

	/* Configure BW control field for the CSn */
	bw &= ~(SROMC_ATTR_MASK << (csn << 2));
	bw |= (attr << (csn << 2));
	writel(bw, S5P_SROM_BW);

	/* Verify SROMC settings */
	bw = __raw_readl(S5P_SROM_BW);
	DPRINTK("%s: new BW setting = 0x%08X\n", __func__, bw);
}

/**
 * sromc_config_access_timing
 * @csn: CSn number
 * @tm_cfg: pointer to an sromc_timing_cfg
 *
 * Configures SROMC access timing register
 *
 */
static void sromc_config_access_timing(unsigned int csn,
				struct sromc_timing_cfg *tm_cfg)
{
	void __iomem *bank_sfr = S5P_SROM_BC0 + (4 * csn);
	unsigned int bc = 0;	/* Bank Control */

	bc = __raw_readl(bank_sfr);
	DPRINTK("%s: old BC%d setting = 0x%08X\n", __func__, csn, bc);

	/* Configure memory access timing for the CSn */
	bc = tm_cfg->tacs | tm_cfg->tcos | tm_cfg->tacc |
	     tm_cfg->tcoh | tm_cfg->tcah | tm_cfg->tacp | tm_cfg->pmc;
	writel(bc, bank_sfr);

	/* Verify SROMC settings */
	bc = __raw_readl(bank_sfr);
	DPRINTK("%s: new BC%d setting = 0x%08X\n", __func__, csn, bc);
}

static struct sromc_bank_cfg tdmb_sromc_bank_cfg = {
	.csn = 0,
	.attr = 0,
};

static struct sromc_timing_cfg tdmb_sromc_timing_cfg = {
	.tacs = 0x0F << 28,
	.tcos = 0x02 << 24,
	.tacc = 0x1F << 16,
	.tcoh = 0x02 << 12,
	.tcah = 0x0F << 8,
	.tacp = 0x00 << 4,
	.pmc  = 0x00 << 0,
};

unsigned long tdmb_get_if_handle(void)
{
	return (unsigned long)v_addr_ebi_cs_base;
}
EXPORT_SYMBOL_GPL(tdmb_get_if_handle);

static int tdmb_ebi_init(struct platform_device *pdev)
{
	struct sromc_bank_cfg *bnk_cfg;
	struct sromc_timing_cfg *tm_cfg;

	if (sromc_enable(pdev) < 0) {
		printk(KERN_DEBUG "tdmb_dev_init sromc_enable fail\n");
		return -1;
	}

	if (sromc_config_demux_gpio(pdev) < 0) {
		printk(KERN_DEBUG "tdmb_dev_init sromc_config_demux_gpio fail\n");
		return -1;
	}

	bnk_cfg = &tdmb_sromc_bank_cfg;
	sromc_config_access_attr(bnk_cfg->csn, bnk_cfg->attr);

	tm_cfg = &tdmb_sromc_timing_cfg;
	sromc_config_access_timing(bnk_cfg->csn, tm_cfg);
	return 0;
}
static struct tdmb_ebi_dt_data *get_ebi_dt_pdata(struct device *dev)
{
	struct tdmb_ebi_dt_data *pdata;

	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata) {
		DPRINTK("%s : could not allocate memory for platform data\n", __func__);
		return NULL;
	}

	if (!of_property_read_u32(dev->of_node, "srom_cs_base", &pdata->cs_base))
		DPRINTK("%s - srom_cs_base : 0x%x\n", __func__, pdata->cs_base);
	else {
		DPRINTK("%s - cs_base missing\n", __func__);
		goto fail;
	}

	if (!of_property_read_u32(dev->of_node, "srom_mem_size", &pdata->mem_size))
		DPRINTK("%s - srom_mem_size : 0x%x\n", __func__, pdata->mem_size);
	else {
		DPRINTK("%s - mem_size missing\n", __func__);
		goto fail;
	}

	return pdata;

fail:
	devm_kfree(dev, pdata);
	return NULL;

}

static int tdmb_sromc_remove(struct platform_device *pdev)
{
	DPRINTK("tdmb_sromc_remove!\n");
	return 0;
}

static int tdmb_sromc_probe(struct platform_device *pdev)
{
	if (pdev->dev.of_node) {
		ebi_dt_pdata = get_ebi_dt_pdata(&pdev->dev);
		if (!ebi_dt_pdata) {
			DPRINTK("%s : ebi_dt_pdata is NULL\n", __func__);
			return -1;
		}
	}

	if (tdmb_ebi_init(pdev) < 0) {
		DPRINTK("%s : tdmb_ebi_init error\n", __func__);
		return -1;
	}

	v_addr_ebi_cs_base = ioremap(ebi_dt_pdata->cs_base, ebi_dt_pdata->mem_size);
	DPRINTK("%s : v_addr_ebi_cs_base 0x%p\n", __func__, v_addr_ebi_cs_base);
	return 0;
}

static const struct of_device_id sromc_match_table[] = {
	{.compatible = "samsung,sromc"},
	{}
};

static struct platform_driver tdmb_ebi_driver = {
	.driver = {
		.name	= "sromc",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(sromc_match_table),
	},
	.remove = tdmb_sromc_remove,
};

int __init tdmb_sromc_init(void)
{
	return platform_driver_probe(&tdmb_ebi_driver, tdmb_sromc_probe);
}
module_init(tdmb_sromc_init);

static void __exit tdmb_sromc_exit(void)
{
	platform_driver_unregister(&tdmb_ebi_driver);
}
module_exit(tdmb_sromc_exit);

MODULE_AUTHOR("Samsung");
MODULE_DESCRIPTION("SROMC Driver");
MODULE_LICENSE("GPL v2");