xref: /openbmc/linux/drivers/memory/ti-aemif.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25a7c8154SIvan Khoronzhuk /*
35a7c8154SIvan Khoronzhuk  * TI AEMIF driver
45a7c8154SIvan Khoronzhuk  *
55a7c8154SIvan Khoronzhuk  * Copyright (C) 2010 - 2013 Texas Instruments Incorporated. http://www.ti.com/
65a7c8154SIvan Khoronzhuk  *
75a7c8154SIvan Khoronzhuk  * Authors:
85a7c8154SIvan Khoronzhuk  * Murali Karicheri <m-karicheri2@ti.com>
95a7c8154SIvan Khoronzhuk  * Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>
105a7c8154SIvan Khoronzhuk  */
115a7c8154SIvan Khoronzhuk 
125a7c8154SIvan Khoronzhuk #include <linux/clk.h>
135a7c8154SIvan Khoronzhuk #include <linux/err.h>
145a7c8154SIvan Khoronzhuk #include <linux/io.h>
155a7c8154SIvan Khoronzhuk #include <linux/kernel.h>
165a7c8154SIvan Khoronzhuk #include <linux/module.h>
175a7c8154SIvan Khoronzhuk #include <linux/of.h>
185a7c8154SIvan Khoronzhuk #include <linux/of_platform.h>
195a7c8154SIvan Khoronzhuk #include <linux/platform_device.h>
20f95bd041SBartosz Golaszewski #include <linux/platform_data/ti-aemif.h>
215a7c8154SIvan Khoronzhuk 
225a7c8154SIvan Khoronzhuk #define TA_SHIFT	2
235a7c8154SIvan Khoronzhuk #define RHOLD_SHIFT	4
245a7c8154SIvan Khoronzhuk #define RSTROBE_SHIFT	7
255a7c8154SIvan Khoronzhuk #define RSETUP_SHIFT	13
265a7c8154SIvan Khoronzhuk #define WHOLD_SHIFT	17
275a7c8154SIvan Khoronzhuk #define WSTROBE_SHIFT	20
285a7c8154SIvan Khoronzhuk #define WSETUP_SHIFT	26
295a7c8154SIvan Khoronzhuk #define EW_SHIFT	30
30fdc482ffSKrzysztof Kozlowski #define SSTROBE_SHIFT	31
315a7c8154SIvan Khoronzhuk 
325a7c8154SIvan Khoronzhuk #define TA(x)		((x) << TA_SHIFT)
335a7c8154SIvan Khoronzhuk #define RHOLD(x)	((x) << RHOLD_SHIFT)
345a7c8154SIvan Khoronzhuk #define RSTROBE(x)	((x) << RSTROBE_SHIFT)
355a7c8154SIvan Khoronzhuk #define RSETUP(x)	((x) << RSETUP_SHIFT)
365a7c8154SIvan Khoronzhuk #define WHOLD(x)	((x) << WHOLD_SHIFT)
375a7c8154SIvan Khoronzhuk #define WSTROBE(x)	((x) << WSTROBE_SHIFT)
385a7c8154SIvan Khoronzhuk #define WSETUP(x)	((x) << WSETUP_SHIFT)
395a7c8154SIvan Khoronzhuk #define EW(x)		((x) << EW_SHIFT)
40fdc482ffSKrzysztof Kozlowski #define SSTROBE(x)	((x) << SSTROBE_SHIFT)
415a7c8154SIvan Khoronzhuk 
425a7c8154SIvan Khoronzhuk #define ASIZE_MAX	0x1
435a7c8154SIvan Khoronzhuk #define TA_MAX		0x3
445a7c8154SIvan Khoronzhuk #define RHOLD_MAX	0x7
455a7c8154SIvan Khoronzhuk #define RSTROBE_MAX	0x3f
465a7c8154SIvan Khoronzhuk #define RSETUP_MAX	0xf
475a7c8154SIvan Khoronzhuk #define WHOLD_MAX	0x7
485a7c8154SIvan Khoronzhuk #define WSTROBE_MAX	0x3f
495a7c8154SIvan Khoronzhuk #define WSETUP_MAX	0xf
505a7c8154SIvan Khoronzhuk #define EW_MAX		0x1
51fdc482ffSKrzysztof Kozlowski #define SSTROBE_MAX	0x1
525a7c8154SIvan Khoronzhuk #define NUM_CS		4
535a7c8154SIvan Khoronzhuk 
545a7c8154SIvan Khoronzhuk #define TA_VAL(x)	(((x) & TA(TA_MAX)) >> TA_SHIFT)
555a7c8154SIvan Khoronzhuk #define RHOLD_VAL(x)	(((x) & RHOLD(RHOLD_MAX)) >> RHOLD_SHIFT)
565a7c8154SIvan Khoronzhuk #define RSTROBE_VAL(x)	(((x) & RSTROBE(RSTROBE_MAX)) >> RSTROBE_SHIFT)
575a7c8154SIvan Khoronzhuk #define RSETUP_VAL(x)	(((x) & RSETUP(RSETUP_MAX)) >> RSETUP_SHIFT)
585a7c8154SIvan Khoronzhuk #define WHOLD_VAL(x)	(((x) & WHOLD(WHOLD_MAX)) >> WHOLD_SHIFT)
595a7c8154SIvan Khoronzhuk #define WSTROBE_VAL(x)	(((x) & WSTROBE(WSTROBE_MAX)) >> WSTROBE_SHIFT)
605a7c8154SIvan Khoronzhuk #define WSETUP_VAL(x)	(((x) & WSETUP(WSETUP_MAX)) >> WSETUP_SHIFT)
615a7c8154SIvan Khoronzhuk #define EW_VAL(x)	(((x) & EW(EW_MAX)) >> EW_SHIFT)
62fdc482ffSKrzysztof Kozlowski #define SSTROBE_VAL(x)	(((x) & SSTROBE(SSTROBE_MAX)) >> SSTROBE_SHIFT)
635a7c8154SIvan Khoronzhuk 
645a7c8154SIvan Khoronzhuk #define NRCSR_OFFSET	0x00
655a7c8154SIvan Khoronzhuk #define AWCCR_OFFSET	0x04
665a7c8154SIvan Khoronzhuk #define A1CR_OFFSET	0x10
675a7c8154SIvan Khoronzhuk 
685a7c8154SIvan Khoronzhuk #define ACR_ASIZE_MASK	0x3
695a7c8154SIvan Khoronzhuk #define ACR_EW_MASK	BIT(30)
70fdc482ffSKrzysztof Kozlowski #define ACR_SSTROBE_MASK	BIT(31)
715a7c8154SIvan Khoronzhuk #define ASIZE_16BIT	1
725a7c8154SIvan Khoronzhuk 
735a7c8154SIvan Khoronzhuk #define CONFIG_MASK	(TA(TA_MAX) | \
745a7c8154SIvan Khoronzhuk 				RHOLD(RHOLD_MAX) | \
755a7c8154SIvan Khoronzhuk 				RSTROBE(RSTROBE_MAX) |	\
765a7c8154SIvan Khoronzhuk 				RSETUP(RSETUP_MAX) | \
775a7c8154SIvan Khoronzhuk 				WHOLD(WHOLD_MAX) | \
785a7c8154SIvan Khoronzhuk 				WSTROBE(WSTROBE_MAX) | \
795a7c8154SIvan Khoronzhuk 				WSETUP(WSETUP_MAX) | \
80fdc482ffSKrzysztof Kozlowski 				EW(EW_MAX) | SSTROBE(SSTROBE_MAX) | \
815a7c8154SIvan Khoronzhuk 				ASIZE_MAX)
825a7c8154SIvan Khoronzhuk 
835a7c8154SIvan Khoronzhuk /**
845a7c8154SIvan Khoronzhuk  * struct aemif_cs_data: structure to hold cs parameters
855a7c8154SIvan Khoronzhuk  * @cs: chip-select number
865a7c8154SIvan Khoronzhuk  * @wstrobe: write strobe width, ns
875a7c8154SIvan Khoronzhuk  * @rstrobe: read strobe width, ns
885a7c8154SIvan Khoronzhuk  * @wsetup: write setup width, ns
895a7c8154SIvan Khoronzhuk  * @whold: write hold width, ns
905a7c8154SIvan Khoronzhuk  * @rsetup: read setup width, ns
915a7c8154SIvan Khoronzhuk  * @rhold: read hold width, ns
925a7c8154SIvan Khoronzhuk  * @ta: minimum turn around time, ns
935a7c8154SIvan Khoronzhuk  * @enable_ss: enable/disable select strobe mode
945a7c8154SIvan Khoronzhuk  * @enable_ew: enable/disable extended wait mode
955a7c8154SIvan Khoronzhuk  * @asize: width of the asynchronous device's data bus
965a7c8154SIvan Khoronzhuk  */
975a7c8154SIvan Khoronzhuk struct aemif_cs_data {
985a7c8154SIvan Khoronzhuk 	u8	cs;
995a7c8154SIvan Khoronzhuk 	u16	wstrobe;
1005a7c8154SIvan Khoronzhuk 	u16	rstrobe;
1015a7c8154SIvan Khoronzhuk 	u8	wsetup;
1025a7c8154SIvan Khoronzhuk 	u8	whold;
1035a7c8154SIvan Khoronzhuk 	u8	rsetup;
1045a7c8154SIvan Khoronzhuk 	u8	rhold;
1055a7c8154SIvan Khoronzhuk 	u8	ta;
1065a7c8154SIvan Khoronzhuk 	u8	enable_ss;
1075a7c8154SIvan Khoronzhuk 	u8	enable_ew;
1085a7c8154SIvan Khoronzhuk 	u8	asize;
1095a7c8154SIvan Khoronzhuk };
1105a7c8154SIvan Khoronzhuk 
1115a7c8154SIvan Khoronzhuk /**
1125a7c8154SIvan Khoronzhuk  * struct aemif_device: structure to hold device data
1135a7c8154SIvan Khoronzhuk  * @base: base address of AEMIF registers
1145a7c8154SIvan Khoronzhuk  * @clk: source clock
1155a7c8154SIvan Khoronzhuk  * @clk_rate: clock's rate in kHz
1165a7c8154SIvan Khoronzhuk  * @num_cs: number of assigned chip-selects
1175a7c8154SIvan Khoronzhuk  * @cs_offset: start number of cs nodes
1185a7c8154SIvan Khoronzhuk  * @cs_data: array of chip-select settings
1195a7c8154SIvan Khoronzhuk  */
1205a7c8154SIvan Khoronzhuk struct aemif_device {
1215a7c8154SIvan Khoronzhuk 	void __iomem *base;
1225a7c8154SIvan Khoronzhuk 	struct clk *clk;
1235a7c8154SIvan Khoronzhuk 	unsigned long clk_rate;
1245a7c8154SIvan Khoronzhuk 	u8 num_cs;
1255a7c8154SIvan Khoronzhuk 	int cs_offset;
1265a7c8154SIvan Khoronzhuk 	struct aemif_cs_data cs_data[NUM_CS];
1275a7c8154SIvan Khoronzhuk };
1285a7c8154SIvan Khoronzhuk 
1295a7c8154SIvan Khoronzhuk /**
1305a7c8154SIvan Khoronzhuk  * aemif_calc_rate - calculate timing data.
1315a7c8154SIvan Khoronzhuk  * @pdev: platform device to calculate for
1325a7c8154SIvan Khoronzhuk  * @wanted: The cycle time needed in nanoseconds.
1335a7c8154SIvan Khoronzhuk  * @clk: The input clock rate in kHz.
1345a7c8154SIvan Khoronzhuk  * @max: The maximum divider value that can be programmed.
1355a7c8154SIvan Khoronzhuk  *
1365a7c8154SIvan Khoronzhuk  * On success, returns the calculated timing value minus 1 for easy
1375a7c8154SIvan Khoronzhuk  * programming into AEMIF timing registers, else negative errno.
1385a7c8154SIvan Khoronzhuk  */
aemif_calc_rate(struct platform_device * pdev,int wanted,unsigned long clk,int max)1395a7c8154SIvan Khoronzhuk static int aemif_calc_rate(struct platform_device *pdev, int wanted,
1405a7c8154SIvan Khoronzhuk 			   unsigned long clk, int max)
1415a7c8154SIvan Khoronzhuk {
1425a7c8154SIvan Khoronzhuk 	int result;
1435a7c8154SIvan Khoronzhuk 
1445a7c8154SIvan Khoronzhuk 	result = DIV_ROUND_UP((wanted * clk), NSEC_PER_MSEC) - 1;
1455a7c8154SIvan Khoronzhuk 
1465a7c8154SIvan Khoronzhuk 	dev_dbg(&pdev->dev, "%s: result %d from %ld, %d\n", __func__, result,
1475a7c8154SIvan Khoronzhuk 		clk, wanted);
1485a7c8154SIvan Khoronzhuk 
1495a7c8154SIvan Khoronzhuk 	/* It is generally OK to have a more relaxed timing than requested... */
1505a7c8154SIvan Khoronzhuk 	if (result < 0)
1515a7c8154SIvan Khoronzhuk 		result = 0;
1525a7c8154SIvan Khoronzhuk 
1535a7c8154SIvan Khoronzhuk 	/* ... But configuring tighter timings is not an option. */
1545a7c8154SIvan Khoronzhuk 	else if (result > max)
1555a7c8154SIvan Khoronzhuk 		result = -EINVAL;
1565a7c8154SIvan Khoronzhuk 
1575a7c8154SIvan Khoronzhuk 	return result;
1585a7c8154SIvan Khoronzhuk }
1595a7c8154SIvan Khoronzhuk 
1605a7c8154SIvan Khoronzhuk /**
1615a7c8154SIvan Khoronzhuk  * aemif_config_abus - configure async bus parameters
1625a7c8154SIvan Khoronzhuk  * @pdev: platform device to configure for
1635a7c8154SIvan Khoronzhuk  * @csnum: aemif chip select number
1645a7c8154SIvan Khoronzhuk  *
1655a7c8154SIvan Khoronzhuk  * This function programs the given timing values (in real clock) into the
1665a7c8154SIvan Khoronzhuk  * AEMIF registers taking the AEMIF clock into account.
1675a7c8154SIvan Khoronzhuk  *
1685a7c8154SIvan Khoronzhuk  * This function does not use any locking while programming the AEMIF
1695a7c8154SIvan Khoronzhuk  * because it is expected that there is only one user of a given
1705a7c8154SIvan Khoronzhuk  * chip-select.
1715a7c8154SIvan Khoronzhuk  *
1725a7c8154SIvan Khoronzhuk  * Returns 0 on success, else negative errno.
1735a7c8154SIvan Khoronzhuk  */
aemif_config_abus(struct platform_device * pdev,int csnum)1745a7c8154SIvan Khoronzhuk static int aemif_config_abus(struct platform_device *pdev, int csnum)
1755a7c8154SIvan Khoronzhuk {
1765a7c8154SIvan Khoronzhuk 	struct aemif_device *aemif = platform_get_drvdata(pdev);
1775a7c8154SIvan Khoronzhuk 	struct aemif_cs_data *data = &aemif->cs_data[csnum];
1785a7c8154SIvan Khoronzhuk 	int ta, rhold, rstrobe, rsetup, whold, wstrobe, wsetup;
1795a7c8154SIvan Khoronzhuk 	unsigned long clk_rate = aemif->clk_rate;
1805a7c8154SIvan Khoronzhuk 	unsigned offset;
1815a7c8154SIvan Khoronzhuk 	u32 set, val;
1825a7c8154SIvan Khoronzhuk 
1835a7c8154SIvan Khoronzhuk 	offset = A1CR_OFFSET + (data->cs - aemif->cs_offset) * 4;
1845a7c8154SIvan Khoronzhuk 
1855a7c8154SIvan Khoronzhuk 	ta	= aemif_calc_rate(pdev, data->ta, clk_rate, TA_MAX);
1865a7c8154SIvan Khoronzhuk 	rhold	= aemif_calc_rate(pdev, data->rhold, clk_rate, RHOLD_MAX);
1875a7c8154SIvan Khoronzhuk 	rstrobe	= aemif_calc_rate(pdev, data->rstrobe, clk_rate, RSTROBE_MAX);
1885a7c8154SIvan Khoronzhuk 	rsetup	= aemif_calc_rate(pdev, data->rsetup, clk_rate, RSETUP_MAX);
1895a7c8154SIvan Khoronzhuk 	whold	= aemif_calc_rate(pdev, data->whold, clk_rate, WHOLD_MAX);
1905a7c8154SIvan Khoronzhuk 	wstrobe	= aemif_calc_rate(pdev, data->wstrobe, clk_rate, WSTROBE_MAX);
1915a7c8154SIvan Khoronzhuk 	wsetup	= aemif_calc_rate(pdev, data->wsetup, clk_rate, WSETUP_MAX);
1925a7c8154SIvan Khoronzhuk 
1935a7c8154SIvan Khoronzhuk 	if (ta < 0 || rhold < 0 || rstrobe < 0 || rsetup < 0 ||
1945a7c8154SIvan Khoronzhuk 	    whold < 0 || wstrobe < 0 || wsetup < 0) {
1955a7c8154SIvan Khoronzhuk 		dev_err(&pdev->dev, "%s: cannot get suitable timings\n",
1965a7c8154SIvan Khoronzhuk 			__func__);
1975a7c8154SIvan Khoronzhuk 		return -EINVAL;
1985a7c8154SIvan Khoronzhuk 	}
1995a7c8154SIvan Khoronzhuk 
2005a7c8154SIvan Khoronzhuk 	set = TA(ta) | RHOLD(rhold) | RSTROBE(rstrobe) | RSETUP(rsetup) |
2015a7c8154SIvan Khoronzhuk 		WHOLD(whold) | WSTROBE(wstrobe) | WSETUP(wsetup);
2025a7c8154SIvan Khoronzhuk 
2035a7c8154SIvan Khoronzhuk 	set |= (data->asize & ACR_ASIZE_MASK);
2045a7c8154SIvan Khoronzhuk 	if (data->enable_ew)
2055a7c8154SIvan Khoronzhuk 		set |= ACR_EW_MASK;
2065a7c8154SIvan Khoronzhuk 	if (data->enable_ss)
207fdc482ffSKrzysztof Kozlowski 		set |= ACR_SSTROBE_MASK;
2085a7c8154SIvan Khoronzhuk 
2095a7c8154SIvan Khoronzhuk 	val = readl(aemif->base + offset);
2105a7c8154SIvan Khoronzhuk 	val &= ~CONFIG_MASK;
2115a7c8154SIvan Khoronzhuk 	val |= set;
2125a7c8154SIvan Khoronzhuk 	writel(val, aemif->base + offset);
2135a7c8154SIvan Khoronzhuk 
2145a7c8154SIvan Khoronzhuk 	return 0;
2155a7c8154SIvan Khoronzhuk }
2165a7c8154SIvan Khoronzhuk 
aemif_cycles_to_nsec(int val,unsigned long clk_rate)2175a7c8154SIvan Khoronzhuk static inline int aemif_cycles_to_nsec(int val, unsigned long clk_rate)
2185a7c8154SIvan Khoronzhuk {
2195a7c8154SIvan Khoronzhuk 	return ((val + 1) * NSEC_PER_MSEC) / clk_rate;
2205a7c8154SIvan Khoronzhuk }
2215a7c8154SIvan Khoronzhuk 
2225a7c8154SIvan Khoronzhuk /**
2235a7c8154SIvan Khoronzhuk  * aemif_get_hw_params - function to read hw register values
2245a7c8154SIvan Khoronzhuk  * @pdev: platform device to read for
2255a7c8154SIvan Khoronzhuk  * @csnum: aemif chip select number
2265a7c8154SIvan Khoronzhuk  *
2275a7c8154SIvan Khoronzhuk  * This function reads the defaults from the registers and update
2285a7c8154SIvan Khoronzhuk  * the timing values. Required for get/set commands and also for
2295a7c8154SIvan Khoronzhuk  * the case when driver needs to use defaults in hardware.
2305a7c8154SIvan Khoronzhuk  */
aemif_get_hw_params(struct platform_device * pdev,int csnum)2315a7c8154SIvan Khoronzhuk static void aemif_get_hw_params(struct platform_device *pdev, int csnum)
2325a7c8154SIvan Khoronzhuk {
2335a7c8154SIvan Khoronzhuk 	struct aemif_device *aemif = platform_get_drvdata(pdev);
2345a7c8154SIvan Khoronzhuk 	struct aemif_cs_data *data = &aemif->cs_data[csnum];
2355a7c8154SIvan Khoronzhuk 	unsigned long clk_rate = aemif->clk_rate;
2365a7c8154SIvan Khoronzhuk 	u32 val, offset;
2375a7c8154SIvan Khoronzhuk 
2385a7c8154SIvan Khoronzhuk 	offset = A1CR_OFFSET + (data->cs - aemif->cs_offset) * 4;
2395a7c8154SIvan Khoronzhuk 	val = readl(aemif->base + offset);
2405a7c8154SIvan Khoronzhuk 
2415a7c8154SIvan Khoronzhuk 	data->ta = aemif_cycles_to_nsec(TA_VAL(val), clk_rate);
2425a7c8154SIvan Khoronzhuk 	data->rhold = aemif_cycles_to_nsec(RHOLD_VAL(val), clk_rate);
2435a7c8154SIvan Khoronzhuk 	data->rstrobe = aemif_cycles_to_nsec(RSTROBE_VAL(val), clk_rate);
2445a7c8154SIvan Khoronzhuk 	data->rsetup = aemif_cycles_to_nsec(RSETUP_VAL(val), clk_rate);
2455a7c8154SIvan Khoronzhuk 	data->whold = aemif_cycles_to_nsec(WHOLD_VAL(val), clk_rate);
2465a7c8154SIvan Khoronzhuk 	data->wstrobe = aemif_cycles_to_nsec(WSTROBE_VAL(val), clk_rate);
2475a7c8154SIvan Khoronzhuk 	data->wsetup = aemif_cycles_to_nsec(WSETUP_VAL(val), clk_rate);
2485a7c8154SIvan Khoronzhuk 	data->enable_ew = EW_VAL(val);
249fdc482ffSKrzysztof Kozlowski 	data->enable_ss = SSTROBE_VAL(val);
2505a7c8154SIvan Khoronzhuk 	data->asize = val & ASIZE_MAX;
2515a7c8154SIvan Khoronzhuk }
2525a7c8154SIvan Khoronzhuk 
2535a7c8154SIvan Khoronzhuk /**
2545a7c8154SIvan Khoronzhuk  * of_aemif_parse_abus_config - parse CS configuration from DT
2555a7c8154SIvan Khoronzhuk  * @pdev: platform device to parse for
2565a7c8154SIvan Khoronzhuk  * @np: device node ptr
2575a7c8154SIvan Khoronzhuk  *
2585a7c8154SIvan Khoronzhuk  * This function update the emif async bus configuration based on the values
2595a7c8154SIvan Khoronzhuk  * configured in a cs device binding node.
2605a7c8154SIvan Khoronzhuk  */
of_aemif_parse_abus_config(struct platform_device * pdev,struct device_node * np)2615a7c8154SIvan Khoronzhuk static int of_aemif_parse_abus_config(struct platform_device *pdev,
2625a7c8154SIvan Khoronzhuk 				      struct device_node *np)
2635a7c8154SIvan Khoronzhuk {
2645a7c8154SIvan Khoronzhuk 	struct aemif_device *aemif = platform_get_drvdata(pdev);
2655a7c8154SIvan Khoronzhuk 	struct aemif_cs_data *data;
2665a7c8154SIvan Khoronzhuk 	u32 cs;
2675a7c8154SIvan Khoronzhuk 	u32 val;
2685a7c8154SIvan Khoronzhuk 
2695a7c8154SIvan Khoronzhuk 	if (of_property_read_u32(np, "ti,cs-chipselect", &cs)) {
2705a7c8154SIvan Khoronzhuk 		dev_dbg(&pdev->dev, "cs property is required");
2715a7c8154SIvan Khoronzhuk 		return -EINVAL;
2725a7c8154SIvan Khoronzhuk 	}
2735a7c8154SIvan Khoronzhuk 
2745a7c8154SIvan Khoronzhuk 	if (cs - aemif->cs_offset >= NUM_CS || cs < aemif->cs_offset) {
2755a7c8154SIvan Khoronzhuk 		dev_dbg(&pdev->dev, "cs number is incorrect %d", cs);
2765a7c8154SIvan Khoronzhuk 		return -EINVAL;
2775a7c8154SIvan Khoronzhuk 	}
2785a7c8154SIvan Khoronzhuk 
2795a7c8154SIvan Khoronzhuk 	if (aemif->num_cs >= NUM_CS) {
2805a7c8154SIvan Khoronzhuk 		dev_dbg(&pdev->dev, "cs count is more than %d", NUM_CS);
2815a7c8154SIvan Khoronzhuk 		return -EINVAL;
2825a7c8154SIvan Khoronzhuk 	}
2835a7c8154SIvan Khoronzhuk 
2845a7c8154SIvan Khoronzhuk 	data = &aemif->cs_data[aemif->num_cs];
2855a7c8154SIvan Khoronzhuk 	data->cs = cs;
2865a7c8154SIvan Khoronzhuk 
2875a7c8154SIvan Khoronzhuk 	/* read the current value in the hw register */
2885a7c8154SIvan Khoronzhuk 	aemif_get_hw_params(pdev, aemif->num_cs++);
2895a7c8154SIvan Khoronzhuk 
2905a7c8154SIvan Khoronzhuk 	/* override the values from device node */
2915a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-min-turnaround-ns", &val))
2925a7c8154SIvan Khoronzhuk 		data->ta = val;
2935a7c8154SIvan Khoronzhuk 
2945a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-read-hold-ns", &val))
2955a7c8154SIvan Khoronzhuk 		data->rhold = val;
2965a7c8154SIvan Khoronzhuk 
2975a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-read-strobe-ns", &val))
2985a7c8154SIvan Khoronzhuk 		data->rstrobe = val;
2995a7c8154SIvan Khoronzhuk 
3005a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-read-setup-ns", &val))
3015a7c8154SIvan Khoronzhuk 		data->rsetup = val;
3025a7c8154SIvan Khoronzhuk 
3035a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-write-hold-ns", &val))
3045a7c8154SIvan Khoronzhuk 		data->whold = val;
3055a7c8154SIvan Khoronzhuk 
3065a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-write-strobe-ns", &val))
3075a7c8154SIvan Khoronzhuk 		data->wstrobe = val;
3085a7c8154SIvan Khoronzhuk 
3095a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-write-setup-ns", &val))
3105a7c8154SIvan Khoronzhuk 		data->wsetup = val;
3115a7c8154SIvan Khoronzhuk 
3125a7c8154SIvan Khoronzhuk 	if (!of_property_read_u32(np, "ti,cs-bus-width", &val))
3135a7c8154SIvan Khoronzhuk 		if (val == 16)
3145a7c8154SIvan Khoronzhuk 			data->asize = 1;
3155a7c8154SIvan Khoronzhuk 	data->enable_ew = of_property_read_bool(np, "ti,cs-extended-wait-mode");
3165a7c8154SIvan Khoronzhuk 	data->enable_ss = of_property_read_bool(np, "ti,cs-select-strobe-mode");
3175a7c8154SIvan Khoronzhuk 	return 0;
3185a7c8154SIvan Khoronzhuk }
3195a7c8154SIvan Khoronzhuk 
3205a7c8154SIvan Khoronzhuk static const struct of_device_id aemif_of_match[] = {
3215a7c8154SIvan Khoronzhuk 	{ .compatible = "ti,davinci-aemif", },
3225a7c8154SIvan Khoronzhuk 	{ .compatible = "ti,da850-aemif", },
3235a7c8154SIvan Khoronzhuk 	{},
3245a7c8154SIvan Khoronzhuk };
325df6be790SLuis de Bethencourt MODULE_DEVICE_TABLE(of, aemif_of_match);
3265a7c8154SIvan Khoronzhuk 
aemif_probe(struct platform_device * pdev)3275a7c8154SIvan Khoronzhuk static int aemif_probe(struct platform_device *pdev)
3285a7c8154SIvan Khoronzhuk {
3295a7c8154SIvan Khoronzhuk 	int i;
3305a7c8154SIvan Khoronzhuk 	int ret = -ENODEV;
3315a7c8154SIvan Khoronzhuk 	struct device *dev = &pdev->dev;
3325a7c8154SIvan Khoronzhuk 	struct device_node *np = dev->of_node;
3335a7c8154SIvan Khoronzhuk 	struct device_node *child_np;
3345a7c8154SIvan Khoronzhuk 	struct aemif_device *aemif;
335f95bd041SBartosz Golaszewski 	struct aemif_platform_data *pdata;
336f95bd041SBartosz Golaszewski 	struct of_dev_auxdata *dev_lookup;
3375a7c8154SIvan Khoronzhuk 
3385a7c8154SIvan Khoronzhuk 	aemif = devm_kzalloc(dev, sizeof(*aemif), GFP_KERNEL);
3395a7c8154SIvan Khoronzhuk 	if (!aemif)
3405a7c8154SIvan Khoronzhuk 		return -ENOMEM;
3415a7c8154SIvan Khoronzhuk 
342f95bd041SBartosz Golaszewski 	pdata = dev_get_platdata(&pdev->dev);
343f95bd041SBartosz Golaszewski 	dev_lookup = pdata ? pdata->dev_lookup : NULL;
344f95bd041SBartosz Golaszewski 
3455a7c8154SIvan Khoronzhuk 	platform_set_drvdata(pdev, aemif);
3465a7c8154SIvan Khoronzhuk 
3475a7c8154SIvan Khoronzhuk 	aemif->clk = devm_clk_get(dev, NULL);
3485a7c8154SIvan Khoronzhuk 	if (IS_ERR(aemif->clk)) {
3495a7c8154SIvan Khoronzhuk 		dev_err(dev, "cannot get clock 'aemif'\n");
3505a7c8154SIvan Khoronzhuk 		return PTR_ERR(aemif->clk);
3515a7c8154SIvan Khoronzhuk 	}
3525a7c8154SIvan Khoronzhuk 
353761d031eSArvind Yadav 	ret = clk_prepare_enable(aemif->clk);
354761d031eSArvind Yadav 	if (ret)
355761d031eSArvind Yadav 		return ret;
356761d031eSArvind Yadav 
3575a7c8154SIvan Khoronzhuk 	aemif->clk_rate = clk_get_rate(aemif->clk) / MSEC_PER_SEC;
3585a7c8154SIvan Khoronzhuk 
3598af70cd2SBartosz Golaszewski 	if (np && of_device_is_compatible(np, "ti,da850-aemif"))
3605a7c8154SIvan Khoronzhuk 		aemif->cs_offset = 2;
3618af70cd2SBartosz Golaszewski 	else if (pdata)
3628af70cd2SBartosz Golaszewski 		aemif->cs_offset = pdata->cs_offset;
3635a7c8154SIvan Khoronzhuk 
364*083008deSKrzysztof Kozlowski 	aemif->base = devm_platform_ioremap_resource(pdev, 0);
3655a7c8154SIvan Khoronzhuk 	if (IS_ERR(aemif->base)) {
3665a7c8154SIvan Khoronzhuk 		ret = PTR_ERR(aemif->base);
3675a7c8154SIvan Khoronzhuk 		goto error;
3685a7c8154SIvan Khoronzhuk 	}
3695a7c8154SIvan Khoronzhuk 
3708af70cd2SBartosz Golaszewski 	if (np) {
3715a7c8154SIvan Khoronzhuk 		/*
3728af70cd2SBartosz Golaszewski 		 * For every controller device node, there is a cs device node
3738af70cd2SBartosz Golaszewski 		 * that describe the bus configuration parameters. This
3748af70cd2SBartosz Golaszewski 		 * functions iterate over these nodes and update the cs data
3758af70cd2SBartosz Golaszewski 		 * array.
3765a7c8154SIvan Khoronzhuk 		 */
3775a7c8154SIvan Khoronzhuk 		for_each_available_child_of_node(np, child_np) {
3785a7c8154SIvan Khoronzhuk 			ret = of_aemif_parse_abus_config(pdev, child_np);
37994e9dd43SPan Bian 			if (ret < 0) {
38094e9dd43SPan Bian 				of_node_put(child_np);
3815a7c8154SIvan Khoronzhuk 				goto error;
3825a7c8154SIvan Khoronzhuk 			}
38394e9dd43SPan Bian 		}
3848af70cd2SBartosz Golaszewski 	} else if (pdata && pdata->num_abus_data > 0) {
3858af70cd2SBartosz Golaszewski 		for (i = 0; i < pdata->num_abus_data; i++, aemif->num_cs++) {
3868af70cd2SBartosz Golaszewski 			aemif->cs_data[i].cs = pdata->abus_data[i].cs;
3878af70cd2SBartosz Golaszewski 			aemif_get_hw_params(pdev, i);
3888af70cd2SBartosz Golaszewski 		}
3898af70cd2SBartosz Golaszewski 	}
3905a7c8154SIvan Khoronzhuk 
3915a7c8154SIvan Khoronzhuk 	for (i = 0; i < aemif->num_cs; i++) {
3925a7c8154SIvan Khoronzhuk 		ret = aemif_config_abus(pdev, i);
3935a7c8154SIvan Khoronzhuk 		if (ret < 0) {
3945a7c8154SIvan Khoronzhuk 			dev_err(dev, "Error configuring chip select %d\n",
3955a7c8154SIvan Khoronzhuk 				aemif->cs_data[i].cs);
3965a7c8154SIvan Khoronzhuk 			goto error;
3975a7c8154SIvan Khoronzhuk 		}
3985a7c8154SIvan Khoronzhuk 	}
3995a7c8154SIvan Khoronzhuk 
4005a7c8154SIvan Khoronzhuk 	/*
4018af70cd2SBartosz Golaszewski 	 * Create a child devices explicitly from here to guarantee that the
4028af70cd2SBartosz Golaszewski 	 * child will be probed after the AEMIF timing parameters are set.
4035a7c8154SIvan Khoronzhuk 	 */
4048af70cd2SBartosz Golaszewski 	if (np) {
4055a7c8154SIvan Khoronzhuk 		for_each_available_child_of_node(np, child_np) {
4068af70cd2SBartosz Golaszewski 			ret = of_platform_populate(child_np, NULL,
4078af70cd2SBartosz Golaszewski 						   dev_lookup, dev);
40894e9dd43SPan Bian 			if (ret < 0) {
40994e9dd43SPan Bian 				of_node_put(child_np);
4105a7c8154SIvan Khoronzhuk 				goto error;
4115a7c8154SIvan Khoronzhuk 			}
41294e9dd43SPan Bian 		}
4136b45a2b1SBartosz Golaszewski 	} else if (pdata) {
4148af70cd2SBartosz Golaszewski 		for (i = 0; i < pdata->num_sub_devices; i++) {
4158af70cd2SBartosz Golaszewski 			pdata->sub_devices[i].dev.parent = dev;
4168af70cd2SBartosz Golaszewski 			ret = platform_device_register(&pdata->sub_devices[i]);
4178af70cd2SBartosz Golaszewski 			if (ret) {
4188af70cd2SBartosz Golaszewski 				dev_warn(dev, "Error register sub device %s\n",
4198af70cd2SBartosz Golaszewski 					 pdata->sub_devices[i].name);
4208af70cd2SBartosz Golaszewski 			}
4218af70cd2SBartosz Golaszewski 		}
4228af70cd2SBartosz Golaszewski 	}
4235a7c8154SIvan Khoronzhuk 
4245a7c8154SIvan Khoronzhuk 	return 0;
4255a7c8154SIvan Khoronzhuk error:
4265a7c8154SIvan Khoronzhuk 	clk_disable_unprepare(aemif->clk);
4275a7c8154SIvan Khoronzhuk 	return ret;
4285a7c8154SIvan Khoronzhuk }
4295a7c8154SIvan Khoronzhuk 
aemif_remove(struct platform_device * pdev)4305a7c8154SIvan Khoronzhuk static int aemif_remove(struct platform_device *pdev)
4315a7c8154SIvan Khoronzhuk {
4325a7c8154SIvan Khoronzhuk 	struct aemif_device *aemif = platform_get_drvdata(pdev);
4335a7c8154SIvan Khoronzhuk 
4345a7c8154SIvan Khoronzhuk 	clk_disable_unprepare(aemif->clk);
4355a7c8154SIvan Khoronzhuk 	return 0;
4365a7c8154SIvan Khoronzhuk }
4375a7c8154SIvan Khoronzhuk 
4385a7c8154SIvan Khoronzhuk static struct platform_driver aemif_driver = {
4395a7c8154SIvan Khoronzhuk 	.probe = aemif_probe,
4405a7c8154SIvan Khoronzhuk 	.remove = aemif_remove,
4415a7c8154SIvan Khoronzhuk 	.driver = {
442f12cb8e2SBartosz Golaszewski 		.name = "ti-aemif",
4435a7c8154SIvan Khoronzhuk 		.of_match_table = of_match_ptr(aemif_of_match),
4445a7c8154SIvan Khoronzhuk 	},
4455a7c8154SIvan Khoronzhuk };
4465a7c8154SIvan Khoronzhuk 
4475a7c8154SIvan Khoronzhuk module_platform_driver(aemif_driver);
4485a7c8154SIvan Khoronzhuk 
4495a7c8154SIvan Khoronzhuk MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
4505a7c8154SIvan Khoronzhuk MODULE_AUTHOR("Ivan Khoronzhuk <ivan.khoronzhuk@ti.com>");
4515a7c8154SIvan Khoronzhuk MODULE_DESCRIPTION("Texas Instruments AEMIF driver");
4525a7c8154SIvan Khoronzhuk MODULE_LICENSE("GPL v2");
4535a7c8154SIvan Khoronzhuk MODULE_ALIAS("platform:" KBUILD_MODNAME);
454