xref: /openbmc/u-boot/drivers/spi/aspeed_spi.c (revision aed21223)
1499853d6Sryan_chen // SPDX-License-Identifier: GPL-2.0+
2499853d6Sryan_chen /*
3499853d6Sryan_chen  * ASPEED AST2500 FMC/SPI Controller driver
4499853d6Sryan_chen  *
5499853d6Sryan_chen  * Copyright (c) 2015-2018, IBM Corporation.
6499853d6Sryan_chen  */
7499853d6Sryan_chen 
8499853d6Sryan_chen #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9499853d6Sryan_chen 
10499853d6Sryan_chen #include <common.h>
11499853d6Sryan_chen #include <clk.h>
12499853d6Sryan_chen #include <dm.h>
13499853d6Sryan_chen #include <spi.h>
14499853d6Sryan_chen #include <spi_flash.h>
15499853d6Sryan_chen #include <asm/io.h>
16499853d6Sryan_chen #include <linux/ioport.h>
17499853d6Sryan_chen 
18499853d6Sryan_chen #define ASPEED_SPI_MAX_CS		3
19beec505fSChin-Ting Kuo #define FLASH_CALIBRATION_LEN   0x400
20499853d6Sryan_chen 
21499853d6Sryan_chen struct aspeed_spi_regs {
22499853d6Sryan_chen 	u32 conf;			/* 0x00 CE Type Setting */
23499853d6Sryan_chen 	u32 ctrl;			/* 0x04 Control */
24499853d6Sryan_chen 	u32 intr_ctrl;			/* 0x08 Interrupt Control and Status */
25499853d6Sryan_chen 	u32 cmd_ctrl;			/* 0x0c Command Control */
26499853d6Sryan_chen 	u32 ce_ctrl[ASPEED_SPI_MAX_CS];	/* 0x10 .. 0x18 CEx Control */
27499853d6Sryan_chen 	u32 _reserved0[5];		/* .. */
28499853d6Sryan_chen 	u32 segment_addr[ASPEED_SPI_MAX_CS];
29499853d6Sryan_chen 					/* 0x30 .. 0x38 Segment Address */
300a73b911SChin-Ting Kuo 	u32 _reserved1[5];		/* .. */
310a73b911SChin-Ting Kuo 	u32 soft_rst_cmd_ctrl;	/* 0x50 Auto Soft-Reset Command Control */
320a73b911SChin-Ting Kuo 	u32 _reserved2[11];		/* .. */
33499853d6Sryan_chen 	u32 dma_ctrl;			/* 0x80 DMA Control/Status */
34499853d6Sryan_chen 	u32 dma_flash_addr;		/* 0x84 DMA Flash Side Address */
35499853d6Sryan_chen 	u32 dma_dram_addr;		/* 0x88 DMA DRAM Side Address */
36499853d6Sryan_chen 	u32 dma_len;			/* 0x8c DMA Length Register */
37499853d6Sryan_chen 	u32 dma_checksum;		/* 0x90 Checksum Calculation Result */
38499853d6Sryan_chen 	u32 timings;			/* 0x94 Read Timing Compensation */
39499853d6Sryan_chen 
40499853d6Sryan_chen 	/* not used */
41499853d6Sryan_chen 	u32 soft_strap_status;		/* 0x9c Software Strap Status */
42499853d6Sryan_chen 	u32 write_cmd_filter_ctrl;	/* 0xa0 Write Command Filter Control */
43499853d6Sryan_chen 	u32 write_addr_filter_ctrl;	/* 0xa4 Write Address Filter Control */
44499853d6Sryan_chen 	u32 lock_ctrl_reset;		/* 0xa8 Lock Control (SRST#) */
45499853d6Sryan_chen 	u32 lock_ctrl_wdt;		/* 0xac Lock Control (Watchdog) */
46499853d6Sryan_chen 	u32 write_addr_filter[5];	/* 0xb0 Write Address Filter */
47499853d6Sryan_chen };
48499853d6Sryan_chen 
49499853d6Sryan_chen /* CE Type Setting Register */
50499853d6Sryan_chen #define CONF_ENABLE_W2			BIT(18)
51499853d6Sryan_chen #define CONF_ENABLE_W1			BIT(17)
52499853d6Sryan_chen #define CONF_ENABLE_W0			BIT(16)
53499853d6Sryan_chen #define CONF_FLASH_TYPE2		4
54499853d6Sryan_chen #define CONF_FLASH_TYPE1		2	/* Hardwired to SPI */
55499853d6Sryan_chen #define CONF_FLASH_TYPE0		0	/* Hardwired to SPI */
56499853d6Sryan_chen #define	  CONF_FLASH_TYPE_NOR		0x0
57499853d6Sryan_chen #define	  CONF_FLASH_TYPE_SPI		0x2
58499853d6Sryan_chen 
59499853d6Sryan_chen /* CE Control Register */
60499853d6Sryan_chen #define CTRL_EXTENDED2			BIT(2)	/* 32 bit addressing for SPI */
61499853d6Sryan_chen #define CTRL_EXTENDED1			BIT(1)	/* 32 bit addressing for SPI */
62499853d6Sryan_chen #define CTRL_EXTENDED0			BIT(0)	/* 32 bit addressing for SPI */
63499853d6Sryan_chen 
64499853d6Sryan_chen /* Interrupt Control and Status Register */
65499853d6Sryan_chen #define INTR_CTRL_DMA_STATUS		BIT(11)
66499853d6Sryan_chen #define INTR_CTRL_CMD_ABORT_STATUS	BIT(10)
67499853d6Sryan_chen #define INTR_CTRL_WRITE_PROTECT_STATUS	BIT(9)
68499853d6Sryan_chen #define INTR_CTRL_DMA_EN		BIT(3)
69499853d6Sryan_chen #define INTR_CTRL_CMD_ABORT_EN		BIT(2)
70499853d6Sryan_chen #define INTR_CTRL_WRITE_PROTECT_EN	BIT(1)
71499853d6Sryan_chen 
72499853d6Sryan_chen /* CEx Control Register */
73499853d6Sryan_chen #define CE_CTRL_IO_MODE_MASK		GENMASK(31, 28)
7476e3c7a9Sryan_chen #define CE_CTRL_IO_QPI_DATA			BIT(31)
75499853d6Sryan_chen #define CE_CTRL_IO_DUAL_DATA		BIT(29)
76499853d6Sryan_chen #define CE_CTRL_IO_DUAL_ADDR_DATA	(BIT(29) | BIT(28))
77499853d6Sryan_chen #define CE_CTRL_IO_QUAD_DATA		BIT(30)
78499853d6Sryan_chen #define CE_CTRL_IO_QUAD_ADDR_DATA	(BIT(30) | BIT(28))
79499853d6Sryan_chen #define CE_CTRL_CMD_SHIFT		16
80499853d6Sryan_chen #define CE_CTRL_CMD_MASK		0xff
81499853d6Sryan_chen #define CE_CTRL_CMD(cmd)					\
82499853d6Sryan_chen 	(((cmd) & CE_CTRL_CMD_MASK) << CE_CTRL_CMD_SHIFT)
83499853d6Sryan_chen #define CE_CTRL_DUMMY_HIGH_SHIFT	14
84499853d6Sryan_chen #define CE_CTRL_DUMMY_HIGH_MASK		0x1
85499853d6Sryan_chen #define CE_CTRL_CLOCK_FREQ_SHIFT	8
86499853d6Sryan_chen #define CE_CTRL_CLOCK_FREQ_MASK		0xf
87499853d6Sryan_chen #define CE_CTRL_CLOCK_FREQ(div)						\
88499853d6Sryan_chen 	(((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT)
897d182336Sryan_chen #define CE_G6_CTRL_CLOCK_FREQ(div)						\
907d182336Sryan_chen 	((((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT) | (((div) & 0xf0) << 20))
91499853d6Sryan_chen #define CE_CTRL_DUMMY_LOW_SHIFT		6 /* 2 bits [7:6] */
92499853d6Sryan_chen #define CE_CTRL_DUMMY_LOW_MASK		0x3
93499853d6Sryan_chen #define CE_CTRL_DUMMY(dummy)						\
94499853d6Sryan_chen 	(((((dummy) >> 2) & CE_CTRL_DUMMY_HIGH_MASK)			\
95499853d6Sryan_chen 	  << CE_CTRL_DUMMY_HIGH_SHIFT) |				\
96499853d6Sryan_chen 	 (((dummy) & CE_CTRL_DUMMY_LOW_MASK) << CE_CTRL_DUMMY_LOW_SHIFT))
97499853d6Sryan_chen #define CE_CTRL_STOP_ACTIVE		BIT(2)
98499853d6Sryan_chen #define CE_CTRL_MODE_MASK		0x3
99499853d6Sryan_chen #define	  CE_CTRL_READMODE		0x0
100499853d6Sryan_chen #define	  CE_CTRL_FREADMODE		0x1
101499853d6Sryan_chen #define	  CE_CTRL_WRITEMODE		0x2
102499853d6Sryan_chen #define	  CE_CTRL_USERMODE		0x3
103499853d6Sryan_chen 
1040a73b911SChin-Ting Kuo /* Auto Soft-Reset Command Control */
1050a73b911SChin-Ting Kuo #define SOFT_RST_CMD_EN     GENMASK(1, 0)
1060a73b911SChin-Ting Kuo 
107499853d6Sryan_chen /*
108499853d6Sryan_chen  * The Segment Register uses a 8MB unit to encode the start address
109499853d6Sryan_chen  * and the end address of the AHB window of a SPI flash device.
110499853d6Sryan_chen  * Default segment addresses are :
111499853d6Sryan_chen  *
112499853d6Sryan_chen  *   CE0  0x20000000 - 0x2fffffff  128MB
113499853d6Sryan_chen  *   CE1  0x28000000 - 0x29ffffff   32MB
114499853d6Sryan_chen  *   CE2  0x2a000000 - 0x2bffffff   32MB
115499853d6Sryan_chen  *
116499853d6Sryan_chen  * The full address space of the AHB window of the controller is
117499853d6Sryan_chen  * covered and CE0 start address and CE2 end addresses are read-only.
118499853d6Sryan_chen  */
119499853d6Sryan_chen #define SEGMENT_ADDR_START(reg)		((((reg) >> 16) & 0xff) << 23)
120499853d6Sryan_chen #define SEGMENT_ADDR_END(reg)		((((reg) >> 24) & 0xff) << 23)
121499853d6Sryan_chen #define SEGMENT_ADDR_VALUE(start, end)					\
122499853d6Sryan_chen 	(((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24))
123499853d6Sryan_chen 
124da83dd7eSryan_chen #define G6_SEGMENT_ADDR_START(reg)		(reg & 0xffff)
125da83dd7eSryan_chen #define G6_SEGMENT_ADDR_END(reg)		((reg >> 16) & 0xffff)
126da83dd7eSryan_chen #define G6_SEGMENT_ADDR_VALUE(start, end)					\
1276167da3dSryan_chen 	((((start) >> 16) & 0xffff) | (((end) - 0x100000) & 0xffff0000))
128da83dd7eSryan_chen 
129499853d6Sryan_chen /* DMA Control/Status Register */
130499853d6Sryan_chen #define DMA_CTRL_DELAY_SHIFT		8
131499853d6Sryan_chen #define DMA_CTRL_DELAY_MASK		0xf
132beec505fSChin-Ting Kuo #define G6_DMA_CTRL_DELAY_MASK		0xff
133499853d6Sryan_chen #define DMA_CTRL_FREQ_SHIFT		4
134f87fadc3Sryan_chen #define G6_DMA_CTRL_FREQ_SHIFT		16
135f87fadc3Sryan_chen 
136499853d6Sryan_chen #define DMA_CTRL_FREQ_MASK		0xf
137499853d6Sryan_chen #define TIMING_MASK(div, delay)					   \
138499853d6Sryan_chen 	(((delay & DMA_CTRL_DELAY_MASK) << DMA_CTRL_DELAY_SHIFT) | \
139499853d6Sryan_chen 	 ((div & DMA_CTRL_FREQ_MASK) << DMA_CTRL_FREQ_SHIFT))
140f87fadc3Sryan_chen #define G6_TIMING_MASK(div, delay)					   \
141beec505fSChin-Ting Kuo 	(((delay & G6_DMA_CTRL_DELAY_MASK) << DMA_CTRL_DELAY_SHIFT) | \
142f87fadc3Sryan_chen 	 ((div & DMA_CTRL_FREQ_MASK) << G6_DMA_CTRL_FREQ_SHIFT))
143499853d6Sryan_chen #define DMA_CTRL_CALIB			BIT(3)
144499853d6Sryan_chen #define DMA_CTRL_CKSUM			BIT(2)
145499853d6Sryan_chen #define DMA_CTRL_WRITE			BIT(1)
146499853d6Sryan_chen #define DMA_CTRL_ENABLE			BIT(0)
147499853d6Sryan_chen 
1480a73b911SChin-Ting Kuo /* for ast2600 setting */
1490a73b911SChin-Ting Kuo #define SPI_3B_AUTO_CLR_REG   0x1e6e2510
1500a73b911SChin-Ting Kuo #define SPI_3B_AUTO_CLR       BIT(9)
1510a73b911SChin-Ting Kuo 
1520a73b911SChin-Ting Kuo 
153499853d6Sryan_chen /*
1540a73b911SChin-Ting Kuo  * flash related info
155499853d6Sryan_chen  */
156499853d6Sryan_chen struct aspeed_spi_flash {
157499853d6Sryan_chen 	u8		cs;
158499853d6Sryan_chen 	bool		init;		/* Initialized when the SPI bus is
159499853d6Sryan_chen 					 * first claimed
160499853d6Sryan_chen 					 */
161499853d6Sryan_chen 	void __iomem	*ahb_base;	/* AHB Window for this device */
162499853d6Sryan_chen 	u32		ahb_size;	/* AHB Window segment size */
163499853d6Sryan_chen 	u32		ce_ctrl_user;	/* CE Control Register for USER mode */
164499853d6Sryan_chen 	u32		ce_ctrl_fread;	/* CE Control Register for FREAD mode */
165499853d6Sryan_chen 	u32		iomode;
166499853d6Sryan_chen 
167499853d6Sryan_chen 	struct spi_flash *spi;		/* Associated SPI Flash device */
168499853d6Sryan_chen };
169499853d6Sryan_chen 
170499853d6Sryan_chen struct aspeed_spi_priv {
171499853d6Sryan_chen 	struct aspeed_spi_regs	*regs;
172499853d6Sryan_chen 	void __iomem	*ahb_base;	/* AHB Window for all flash devices */
173499853d6Sryan_chen 	int new_ver;
174499853d6Sryan_chen 	u32		ahb_size;	/* AHB Window segments size */
175499853d6Sryan_chen 
176499853d6Sryan_chen 	ulong		hclk_rate;	/* AHB clock rate */
177499853d6Sryan_chen 	u32		max_hz;
178499853d6Sryan_chen 	u8		num_cs;
179499853d6Sryan_chen 	bool		is_fmc;
180499853d6Sryan_chen 
181499853d6Sryan_chen 	struct aspeed_spi_flash flashes[ASPEED_SPI_MAX_CS];
182499853d6Sryan_chen 	u32		flash_count;
183499853d6Sryan_chen 
184499853d6Sryan_chen 	u8		cmd_buf[16];	/* SPI command in progress */
185499853d6Sryan_chen 	size_t		cmd_len;
186499853d6Sryan_chen };
187499853d6Sryan_chen 
188499853d6Sryan_chen static struct aspeed_spi_flash *aspeed_spi_get_flash(struct udevice *dev)
189499853d6Sryan_chen {
190499853d6Sryan_chen 	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
191499853d6Sryan_chen 	struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
192499853d6Sryan_chen 	u8 cs = slave_plat->cs;
193499853d6Sryan_chen 
194499853d6Sryan_chen 	if (cs >= priv->flash_count) {
195499853d6Sryan_chen 		pr_err("invalid CS %u\n", cs);
196499853d6Sryan_chen 		return NULL;
197499853d6Sryan_chen 	}
198499853d6Sryan_chen 
199499853d6Sryan_chen 	return &priv->flashes[cs];
200499853d6Sryan_chen }
201499853d6Sryan_chen 
202ac86fa8bSryan_chen static u32 aspeed_g6_spi_hclk_divisor(struct aspeed_spi_priv *priv, u32 max_hz)
203499853d6Sryan_chen {
2047d182336Sryan_chen 	u32 hclk_rate = priv->hclk_rate;
205499853d6Sryan_chen 	/* HCLK/1 ..	HCLK/16 */
206499853d6Sryan_chen 	const u8 hclk_masks[] = {
207499853d6Sryan_chen 		15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0
208499853d6Sryan_chen 	};
2097d182336Sryan_chen 	u8 base_div = 0;
210f87fadc3Sryan_chen 	int done = 0;
211f87fadc3Sryan_chen 	u32 i, j = 0;
2127d182336Sryan_chen 	u32 hclk_div_setting = 0;
213499853d6Sryan_chen 
214f87fadc3Sryan_chen 	for (j = 0; j < 0xf; i++) {
2157d182336Sryan_chen 		for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
2167d182336Sryan_chen 			base_div = j * 16;
217f87fadc3Sryan_chen 			if (max_hz >= (hclk_rate / ((i + 1) + base_div))) {
218f87fadc3Sryan_chen 
219f87fadc3Sryan_chen 				done = 1;
2207d182336Sryan_chen 				break;
2217d182336Sryan_chen 			}
2227d182336Sryan_chen 		}
223f87fadc3Sryan_chen 			if (done)
224f87fadc3Sryan_chen 				break;
2257d182336Sryan_chen 	}
226499853d6Sryan_chen 
227f87fadc3Sryan_chen 	debug("hclk=%d required=%d h_div %d, divisor is %d (mask %x) speed=%d\n",
228f87fadc3Sryan_chen 		  hclk_rate, max_hz, j, i + 1, hclk_masks[i], hclk_rate / (i + 1 + base_div));
2297d182336Sryan_chen 
2307d182336Sryan_chen 	hclk_div_setting = ((j << 4) | hclk_masks[i]);
2317d182336Sryan_chen 
232ac86fa8bSryan_chen 	return hclk_div_setting;
233ac86fa8bSryan_chen 
234ac86fa8bSryan_chen }
235ac86fa8bSryan_chen 
236ac86fa8bSryan_chen static u32 aspeed_spi_hclk_divisor(struct aspeed_spi_priv *priv, u32 max_hz)
237ac86fa8bSryan_chen {
238ac86fa8bSryan_chen 	u32 hclk_rate = priv->hclk_rate;
239ac86fa8bSryan_chen 	/* HCLK/1 ..	HCLK/16 */
240ac86fa8bSryan_chen 	const u8 hclk_masks[] = {
241ac86fa8bSryan_chen 		15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0
242ac86fa8bSryan_chen 	};
243d32338fdSryan_chen 	u32 i;
244ac86fa8bSryan_chen 	u32 hclk_div_setting = 0;
245ac86fa8bSryan_chen 
246499853d6Sryan_chen 	for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
247499853d6Sryan_chen 		if (max_hz >= (hclk_rate / (i + 1)))
248499853d6Sryan_chen 			break;
249499853d6Sryan_chen 	}
250499853d6Sryan_chen 	debug("hclk=%d required=%d divisor is %d (mask %x) speed=%d\n",
251499853d6Sryan_chen 	      hclk_rate, max_hz, i + 1, hclk_masks[i], hclk_rate / (i + 1));
252499853d6Sryan_chen 
2537d182336Sryan_chen 	hclk_div_setting = hclk_masks[i];
2547d182336Sryan_chen 
2557d182336Sryan_chen 	return hclk_div_setting;
256499853d6Sryan_chen }
257499853d6Sryan_chen 
258499853d6Sryan_chen /*
259499853d6Sryan_chen  * Use some address/size under the first flash device CE0
260499853d6Sryan_chen  */
261499853d6Sryan_chen static u32 aspeed_spi_fmc_checksum(struct aspeed_spi_priv *priv, u8 div,
262499853d6Sryan_chen 				   u8 delay)
263499853d6Sryan_chen {
264499853d6Sryan_chen 	u32 flash_addr = (u32)priv->ahb_base + 0x10000;
265beec505fSChin-Ting Kuo 	u32 flash_len = FLASH_CALIBRATION_LEN;
266499853d6Sryan_chen 	u32 dma_ctrl;
267499853d6Sryan_chen 	u32 checksum;
268499853d6Sryan_chen 
269499853d6Sryan_chen 	writel(flash_addr, &priv->regs->dma_flash_addr);
270499853d6Sryan_chen 	writel(flash_len,  &priv->regs->dma_len);
271499853d6Sryan_chen 
272499853d6Sryan_chen 	/*
273499853d6Sryan_chen 	 * When doing calibration, the SPI clock rate in the CE0
274499853d6Sryan_chen 	 * Control Register and the data input delay cycles in the
275499853d6Sryan_chen 	 * Read Timing Compensation Register are replaced by bit[11:4].
276499853d6Sryan_chen 	 */
277f87fadc3Sryan_chen 	if(priv->new_ver)
278f87fadc3Sryan_chen 		dma_ctrl = DMA_CTRL_ENABLE | DMA_CTRL_CKSUM | DMA_CTRL_CALIB |
279f87fadc3Sryan_chen 			G6_TIMING_MASK(div, delay);
280f87fadc3Sryan_chen 	else
281499853d6Sryan_chen 		dma_ctrl = DMA_CTRL_ENABLE | DMA_CTRL_CKSUM | DMA_CTRL_CALIB |
282499853d6Sryan_chen 			TIMING_MASK(div, delay);
283499853d6Sryan_chen 	writel(dma_ctrl, &priv->regs->dma_ctrl);
284499853d6Sryan_chen 	while (!(readl(&priv->regs->intr_ctrl) & INTR_CTRL_DMA_STATUS))
285499853d6Sryan_chen 		;
286499853d6Sryan_chen 
287499853d6Sryan_chen 	writel(0x0, &priv->regs->intr_ctrl);
288499853d6Sryan_chen 
289499853d6Sryan_chen 	checksum = readl(&priv->regs->dma_checksum);
290499853d6Sryan_chen 
291499853d6Sryan_chen 	writel(0x0, &priv->regs->dma_ctrl);
292499853d6Sryan_chen 	return checksum;
293499853d6Sryan_chen }
294499853d6Sryan_chen 
295499853d6Sryan_chen static u32 aspeed_spi_read_checksum(struct aspeed_spi_priv *priv, u8 div,
296499853d6Sryan_chen 				    u8 delay)
297499853d6Sryan_chen {
298499853d6Sryan_chen 	/* TODO(clg@kaod.org): the SPI controllers do not have the DMA
299499853d6Sryan_chen 	 * registers. The algorithm is the same.
300499853d6Sryan_chen 	 */
301499853d6Sryan_chen 	if (!priv->is_fmc) {
302499853d6Sryan_chen 		pr_warn("No timing calibration support for SPI controllers");
303499853d6Sryan_chen 		return 0xbadc0de;
304499853d6Sryan_chen 	}
305499853d6Sryan_chen 
306499853d6Sryan_chen 	return aspeed_spi_fmc_checksum(priv, div, delay);
307499853d6Sryan_chen }
308499853d6Sryan_chen 
309499853d6Sryan_chen #define TIMING_DELAY_DI_4NS         BIT(3)
310499853d6Sryan_chen #define TIMING_DELAY_HCYCLE_MAX     5
311499853d6Sryan_chen 
312499853d6Sryan_chen static int aspeed_spi_timing_calibration(struct aspeed_spi_priv *priv)
313499853d6Sryan_chen {
314499853d6Sryan_chen 	/* HCLK/5 .. HCLK/1 */
315499853d6Sryan_chen 	const u8 hclk_masks[] = {13, 6, 14, 7, 15};
316beec505fSChin-Ting Kuo 	u32 timing_reg;
317499853d6Sryan_chen 	u32 checksum, gold_checksum;
318f87fadc3Sryan_chen 	int i, hcycle, delay_ns;
319beec505fSChin-Ting Kuo 
320beec505fSChin-Ting Kuo 	/* Use the ctrl setting in aspeed_spi_flash_init() to
321beec505fSChin-Ting Kuo 	 * implement calibration process.
322beec505fSChin-Ting Kuo 	 */
323beec505fSChin-Ting Kuo 	timing_reg = readl(&priv->regs->timings);
324beec505fSChin-Ting Kuo 	if (timing_reg != 0)
325beec505fSChin-Ting Kuo 		return 0;
326499853d6Sryan_chen 
327499853d6Sryan_chen 	debug("Read timing calibration :\n");
328499853d6Sryan_chen 
329499853d6Sryan_chen 	/* Compute reference checksum at lowest freq HCLK/16 */
330499853d6Sryan_chen 	gold_checksum = aspeed_spi_read_checksum(priv, 0, 0);
331499853d6Sryan_chen 
332499853d6Sryan_chen 	/* Increase HCLK freq */
3337d182336Sryan_chen 	if (priv->new_ver) {
3347d182336Sryan_chen 		for (i = 0; i < ARRAY_SIZE(hclk_masks) - 1; i++) {
3357d182336Sryan_chen 			u32 hdiv = 5 - i;
336f87fadc3Sryan_chen 			u32 hshift = (hdiv - 2) * 8;
3377d182336Sryan_chen 			bool pass = false;
3387d182336Sryan_chen 			u8 delay;
339f87fadc3Sryan_chen 			u16 first_delay = 0;
340f87fadc3Sryan_chen 			u16 end_delay = 0;
341f87fadc3Sryan_chen 			u32 cal_tmp;
342beec505fSChin-Ting Kuo 			u32 max_window_sz = 0;
343beec505fSChin-Ting Kuo 			u32 cur_window_sz = 0;
344beec505fSChin-Ting Kuo 			u32 tmp_delay;
345beec505fSChin-Ting Kuo 
346f87fadc3Sryan_chen 			debug("hdiv %d, hshift %d\n", hdiv, hshift);
3477d182336Sryan_chen 			if (priv->hclk_rate / hdiv > priv->max_hz) {
3487d182336Sryan_chen 				debug("skipping freq %ld\n", priv->hclk_rate / hdiv);
3497d182336Sryan_chen 				continue;
3507d182336Sryan_chen 			}
3517d182336Sryan_chen 
3527d182336Sryan_chen 			/* Try without the 4ns DI delay */
353f87fadc3Sryan_chen 			hcycle = delay = 0;
354f87fadc3Sryan_chen 			debug("** Dealy Disable **\n");
355f87fadc3Sryan_chen 			checksum = aspeed_spi_read_checksum(priv, hclk_masks[i], delay);
3567d182336Sryan_chen 			pass = (checksum == gold_checksum);
357d32338fdSryan_chen 			debug("HCLK/%d, no DI delay, %d HCLK cycle : %s\n",
3587d182336Sryan_chen 				  hdiv, hcycle, pass ? "PASS" : "FAIL");
3597d182336Sryan_chen 
3607d182336Sryan_chen 			/* All good for this freq  */
3617d182336Sryan_chen 			if (pass)
362f87fadc3Sryan_chen 				goto next_div;
363f87fadc3Sryan_chen 
364beec505fSChin-Ting Kuo 			/* Try each hcycle delay */
365f87fadc3Sryan_chen 			for (hcycle = 0; hcycle <= TIMING_DELAY_HCYCLE_MAX; hcycle++) {
366beec505fSChin-Ting Kuo 				/* Increase DI delay by the step of 0.5ns */
367f87fadc3Sryan_chen 				debug("** Delay Enable : hcycle %x ** \n", hcycle);
368f87fadc3Sryan_chen 				for (delay_ns = 0; delay_ns < 0xf; delay_ns++) {
369beec505fSChin-Ting Kuo 					tmp_delay = TIMING_DELAY_DI_4NS | hcycle | (delay_ns << 4);
370f87fadc3Sryan_chen 					checksum = aspeed_spi_read_checksum(priv, hclk_masks[i],
371beec505fSChin-Ting Kuo 									    tmp_delay);
372f87fadc3Sryan_chen 					pass = (checksum == gold_checksum);
373beec505fSChin-Ting Kuo 					debug("HCLK/%d, DI delay, %d HCLK cycle, %d delay_ns : %s\n",
374f87fadc3Sryan_chen 					      hdiv, hcycle, delay_ns, pass ? "PASS" : "FAIL");
375f87fadc3Sryan_chen 
376f87fadc3Sryan_chen 					if (!pass) {
377f87fadc3Sryan_chen 						if (!first_delay)
378f87fadc3Sryan_chen 							continue;
379f87fadc3Sryan_chen 						else {
380f87fadc3Sryan_chen 							end_delay = (hcycle << 4) | (delay_ns);
381f87fadc3Sryan_chen 							end_delay = end_delay - 1;
382beec505fSChin-Ting Kuo 							/* Larger window size is found */
383beec505fSChin-Ting Kuo 							if (cur_window_sz > max_window_sz) {
384beec505fSChin-Ting Kuo 								max_window_sz = cur_window_sz;
385beec505fSChin-Ting Kuo 								cal_tmp = (first_delay + end_delay) / 2;
386beec505fSChin-Ting Kuo 								delay = TIMING_DELAY_DI_4NS |
387beec505fSChin-Ting Kuo 										((cal_tmp & 0xf) << 4) |
388beec505fSChin-Ting Kuo 										(cal_tmp >> 4);
389beec505fSChin-Ting Kuo 							}
390beec505fSChin-Ting Kuo 							debug("find end_delay %x %d %d\n", end_delay,
391beec505fSChin-Ting Kuo 									hcycle, delay_ns);
392beec505fSChin-Ting Kuo 
393beec505fSChin-Ting Kuo 							first_delay = 0;
394beec505fSChin-Ting Kuo 							end_delay = 0;
395beec505fSChin-Ting Kuo 							cur_window_sz = 0;
396beec505fSChin-Ting Kuo 
3977d182336Sryan_chen 							break;
3987d182336Sryan_chen 						}
399f87fadc3Sryan_chen 					} else {
400f87fadc3Sryan_chen 						if (!first_delay) {
401f87fadc3Sryan_chen 							first_delay = (hcycle << 4) | delay_ns;
402f87fadc3Sryan_chen 							debug("find first_delay %x %d %d\n", first_delay, hcycle, delay_ns);
403f87fadc3Sryan_chen 						}
404beec505fSChin-Ting Kuo 						/* Record current pass window size */
405beec505fSChin-Ting Kuo 						cur_window_sz++;
406beec505fSChin-Ting Kuo 					}
407f87fadc3Sryan_chen 				}
408f87fadc3Sryan_chen 			}
4097d182336Sryan_chen 
4107d182336Sryan_chen 			if (pass) {
411beec505fSChin-Ting Kuo 				if (cur_window_sz > max_window_sz) {
412beec505fSChin-Ting Kuo 					max_window_sz = cur_window_sz;
413beec505fSChin-Ting Kuo 					end_delay = ((hcycle - 1) << 4) | (delay_ns - 1);
414f87fadc3Sryan_chen 					cal_tmp = (first_delay + end_delay) / 2;
415beec505fSChin-Ting Kuo 					delay = TIMING_DELAY_DI_4NS |
416beec505fSChin-Ting Kuo 							((cal_tmp & 0xf) << 4) |
417beec505fSChin-Ting Kuo 							(cal_tmp >> 4);
418f87fadc3Sryan_chen 				}
419f87fadc3Sryan_chen 			}
420f87fadc3Sryan_chen next_div:
4217d182336Sryan_chen 			timing_reg &= ~(0xfu << hshift);
4227d182336Sryan_chen 			timing_reg |= delay << hshift;
423f87fadc3Sryan_chen 			debug("timing_reg %x, delay %x, hshift bit %d\n",timing_reg, delay, hshift);
4247d182336Sryan_chen 		}
4257d182336Sryan_chen 	} else {
426499853d6Sryan_chen 		for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
427499853d6Sryan_chen 			u32 hdiv = 5 - i;
428499853d6Sryan_chen 			u32 hshift = (hdiv - 1) << 2;
429499853d6Sryan_chen 			bool pass = false;
430499853d6Sryan_chen 			u8 delay;
431499853d6Sryan_chen 
432499853d6Sryan_chen 			if (priv->hclk_rate / hdiv > priv->max_hz) {
433499853d6Sryan_chen 				debug("skipping freq %ld\n", priv->hclk_rate / hdiv);
434499853d6Sryan_chen 				continue;
435499853d6Sryan_chen 			}
436499853d6Sryan_chen 
437499853d6Sryan_chen 			/* Increase HCLK cycles until read succeeds */
438499853d6Sryan_chen 			for (hcycle = 0; hcycle <= TIMING_DELAY_HCYCLE_MAX; hcycle++) {
439499853d6Sryan_chen 				/* Try first with a 4ns DI delay */
440499853d6Sryan_chen 				delay = TIMING_DELAY_DI_4NS | hcycle;
441499853d6Sryan_chen 				checksum = aspeed_spi_read_checksum(priv, hclk_masks[i],
442499853d6Sryan_chen 								    delay);
443499853d6Sryan_chen 				pass = (checksum == gold_checksum);
444499853d6Sryan_chen 				debug(" HCLK/%d, 4ns DI delay, %d HCLK cycle : %s\n",
445499853d6Sryan_chen 				      hdiv, hcycle, pass ? "PASS" : "FAIL");
446499853d6Sryan_chen 
447499853d6Sryan_chen 				/* Try again with more HCLK cycles */
448499853d6Sryan_chen 				if (!pass)
449499853d6Sryan_chen 					continue;
450499853d6Sryan_chen 
451499853d6Sryan_chen 				/* Try without the 4ns DI delay */
452499853d6Sryan_chen 				delay = hcycle;
453499853d6Sryan_chen 				checksum = aspeed_spi_read_checksum(priv, hclk_masks[i],
454499853d6Sryan_chen 								    delay);
455499853d6Sryan_chen 				pass = (checksum == gold_checksum);
456499853d6Sryan_chen 				debug(" HCLK/%d,  no DI delay, %d HCLK cycle : %s\n",
457499853d6Sryan_chen 				      hdiv, hcycle, pass ? "PASS" : "FAIL");
458499853d6Sryan_chen 
459499853d6Sryan_chen 				/* All good for this freq  */
460499853d6Sryan_chen 				if (pass)
461499853d6Sryan_chen 					break;
462499853d6Sryan_chen 			}
463499853d6Sryan_chen 
464499853d6Sryan_chen 			if (pass) {
465499853d6Sryan_chen 				timing_reg &= ~(0xfu << hshift);
466499853d6Sryan_chen 				timing_reg |= delay << hshift;
467499853d6Sryan_chen 			}
468499853d6Sryan_chen 		}
4697d182336Sryan_chen 	}
470beec505fSChin-Ting Kuo 
471499853d6Sryan_chen 	debug("Read Timing Compensation set to 0x%08x\n", timing_reg);
472499853d6Sryan_chen 	writel(timing_reg, &priv->regs->timings);
473499853d6Sryan_chen 
474499853d6Sryan_chen 	return 0;
475499853d6Sryan_chen }
476499853d6Sryan_chen 
477499853d6Sryan_chen static int aspeed_spi_controller_init(struct aspeed_spi_priv *priv)
478499853d6Sryan_chen {
479beec505fSChin-Ting Kuo 	int cs;
480499853d6Sryan_chen 
481499853d6Sryan_chen 	/*
482499853d6Sryan_chen 	 * Enable write on all flash devices as USER command mode
483499853d6Sryan_chen 	 * requires it.
484499853d6Sryan_chen 	 */
485499853d6Sryan_chen 	setbits_le32(&priv->regs->conf,
486499853d6Sryan_chen 		     CONF_ENABLE_W2 | CONF_ENABLE_W1 | CONF_ENABLE_W0);
487499853d6Sryan_chen 
488499853d6Sryan_chen 	/*
489499853d6Sryan_chen 	 * Set safe default settings for each device. These will be
490499853d6Sryan_chen 	 * tuned after the SPI flash devices are probed.
491499853d6Sryan_chen 	 */
492da83dd7eSryan_chen 	if (priv->new_ver) {
493499853d6Sryan_chen 		for (cs = 0; cs < priv->flash_count; cs++) {
494499853d6Sryan_chen 			struct aspeed_spi_flash *flash = &priv->flashes[cs];
495499853d6Sryan_chen 			u32 seg_addr = readl(&priv->regs->segment_addr[cs]);
496d32338fdSryan_chen 			u32 addr_config = 0;
497499853d6Sryan_chen 			switch(cs) {
498da83dd7eSryan_chen 				case 0:
499da83dd7eSryan_chen 					flash->ahb_base = cs ? (void *)G6_SEGMENT_ADDR_START(seg_addr) :
500da83dd7eSryan_chen 						priv->ahb_base;
501d32338fdSryan_chen 					debug("cs0 mem-map : %x \n", (u32)flash->ahb_base);
502da83dd7eSryan_chen 					break;
503499853d6Sryan_chen 				case 1:
504*aed21223SChin-Ting Kuo 					flash->ahb_base = priv->flashes[0].ahb_base + 0x8000000;	//cs0 + 128Mb : use 64MB
5056167da3dSryan_chen 					debug("cs1 mem-map : %x end %x \n", (u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000);
5066424f9abSChin-Ting Kuo 					addr_config = G6_SEGMENT_ADDR_VALUE((u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000); //add 512Mb
507d32338fdSryan_chen 					writel(addr_config, &priv->regs->segment_addr[cs]);
508499853d6Sryan_chen 					break;
509499853d6Sryan_chen 				case 2:
5106167da3dSryan_chen 					flash->ahb_base = priv->flashes[0].ahb_base + 0xc000000;	//cs0 + 192Mb : use 64MB
5116167da3dSryan_chen 					debug("cs2 mem-map : %x end %x \n", (u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000);
5126424f9abSChin-Ting Kuo 					addr_config = G6_SEGMENT_ADDR_VALUE((u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000); //add 512Mb
5136167da3dSryan_chen 					writel(addr_config, &priv->regs->segment_addr[cs]);
514499853d6Sryan_chen 					break;
515499853d6Sryan_chen 			}
516da83dd7eSryan_chen 			flash->cs = cs;
517da83dd7eSryan_chen 			flash->ce_ctrl_user = CE_CTRL_USERMODE;
518da83dd7eSryan_chen 			flash->ce_ctrl_fread = CE_CTRL_READMODE;
519499853d6Sryan_chen 		}
520da83dd7eSryan_chen 	} else {
521da83dd7eSryan_chen 		for (cs = 0; cs < priv->flash_count; cs++) {
522da83dd7eSryan_chen 			struct aspeed_spi_flash *flash = &priv->flashes[cs];
523da83dd7eSryan_chen 			u32 seg_addr = readl(&priv->regs->segment_addr[cs]);
524499853d6Sryan_chen 			/*
525499853d6Sryan_chen 			 * The start address of the AHB window of CE0 is
526499853d6Sryan_chen 			 * read-only and is the same as the address of the
527499853d6Sryan_chen 			 * overall AHB window of the controller for all flash
528499853d6Sryan_chen 			 * devices.
529499853d6Sryan_chen 			 */
530499853d6Sryan_chen 			flash->ahb_base = cs ? (void *)SEGMENT_ADDR_START(seg_addr) :
531499853d6Sryan_chen 				priv->ahb_base;
532499853d6Sryan_chen 
533499853d6Sryan_chen 			flash->cs = cs;
534499853d6Sryan_chen 			flash->ce_ctrl_user = CE_CTRL_USERMODE;
535499853d6Sryan_chen 			flash->ce_ctrl_fread = CE_CTRL_READMODE;
536499853d6Sryan_chen 		}
537da83dd7eSryan_chen 	}
538499853d6Sryan_chen 	return 0;
539499853d6Sryan_chen }
540499853d6Sryan_chen 
541499853d6Sryan_chen static int aspeed_spi_read_from_ahb(void __iomem *ahb_base, void *buf,
542499853d6Sryan_chen 				    size_t len)
543499853d6Sryan_chen {
544499853d6Sryan_chen 	size_t offset = 0;
545499853d6Sryan_chen 
546499853d6Sryan_chen 	if (!((uintptr_t)buf % 4)) {
547499853d6Sryan_chen 		readsl(ahb_base, buf, len >> 2);
548499853d6Sryan_chen 		offset = len & ~0x3;
549499853d6Sryan_chen 		len -= offset;
550499853d6Sryan_chen 	}
551499853d6Sryan_chen 	readsb(ahb_base, (u8 *)buf + offset, len);
552499853d6Sryan_chen 
553499853d6Sryan_chen 	return 0;
554499853d6Sryan_chen }
555499853d6Sryan_chen 
556499853d6Sryan_chen static int aspeed_spi_write_to_ahb(void __iomem *ahb_base, const void *buf,
557499853d6Sryan_chen 				   size_t len)
558499853d6Sryan_chen {
559499853d6Sryan_chen 	size_t offset = 0;
560499853d6Sryan_chen 
561499853d6Sryan_chen 	if (!((uintptr_t)buf % 4)) {
562499853d6Sryan_chen 		writesl(ahb_base, buf, len >> 2);
563499853d6Sryan_chen 		offset = len & ~0x3;
564499853d6Sryan_chen 		len -= offset;
565499853d6Sryan_chen 	}
566499853d6Sryan_chen 	writesb(ahb_base, (u8 *)buf + offset, len);
567499853d6Sryan_chen 
568499853d6Sryan_chen 	return 0;
569499853d6Sryan_chen }
570499853d6Sryan_chen 
571499853d6Sryan_chen static void aspeed_spi_start_user(struct aspeed_spi_priv *priv,
572499853d6Sryan_chen 				  struct aspeed_spi_flash *flash)
573499853d6Sryan_chen {
574499853d6Sryan_chen 	u32 ctrl_reg = flash->ce_ctrl_user | CE_CTRL_STOP_ACTIVE;
575499853d6Sryan_chen 
576499853d6Sryan_chen 	/* Deselect CS and set USER command mode */
577499853d6Sryan_chen 	writel(ctrl_reg, &priv->regs->ce_ctrl[flash->cs]);
578499853d6Sryan_chen 
579499853d6Sryan_chen 	/* Select CS */
580499853d6Sryan_chen 	clrbits_le32(&priv->regs->ce_ctrl[flash->cs], CE_CTRL_STOP_ACTIVE);
581499853d6Sryan_chen }
582499853d6Sryan_chen 
583499853d6Sryan_chen static void aspeed_spi_stop_user(struct aspeed_spi_priv *priv,
584499853d6Sryan_chen 				 struct aspeed_spi_flash *flash)
585499853d6Sryan_chen {
586499853d6Sryan_chen 	/* Deselect CS first */
587499853d6Sryan_chen 	setbits_le32(&priv->regs->ce_ctrl[flash->cs], CE_CTRL_STOP_ACTIVE);
588499853d6Sryan_chen 
589499853d6Sryan_chen 	/* Restore default command mode */
590499853d6Sryan_chen 	writel(flash->ce_ctrl_fread, &priv->regs->ce_ctrl[flash->cs]);
591499853d6Sryan_chen }
592499853d6Sryan_chen 
593499853d6Sryan_chen static int aspeed_spi_read_reg(struct aspeed_spi_priv *priv,
594499853d6Sryan_chen 			       struct aspeed_spi_flash *flash,
595499853d6Sryan_chen 			       u8 opcode, u8 *read_buf, int len)
596499853d6Sryan_chen {
597499853d6Sryan_chen 	aspeed_spi_start_user(priv, flash);
598499853d6Sryan_chen 	aspeed_spi_write_to_ahb(flash->ahb_base, &opcode, 1);
599499853d6Sryan_chen 	aspeed_spi_read_from_ahb(flash->ahb_base, read_buf, len);
600499853d6Sryan_chen 	aspeed_spi_stop_user(priv, flash);
601499853d6Sryan_chen 
602499853d6Sryan_chen 	return 0;
603499853d6Sryan_chen }
604499853d6Sryan_chen 
605499853d6Sryan_chen static int aspeed_spi_write_reg(struct aspeed_spi_priv *priv,
606499853d6Sryan_chen 				struct aspeed_spi_flash *flash,
607499853d6Sryan_chen 				u8 opcode, const u8 *write_buf, int len)
608499853d6Sryan_chen {
609499853d6Sryan_chen 	aspeed_spi_start_user(priv, flash);
610499853d6Sryan_chen 	aspeed_spi_write_to_ahb(flash->ahb_base, &opcode, 1);
611499853d6Sryan_chen 	aspeed_spi_write_to_ahb(flash->ahb_base, write_buf, len);
612499853d6Sryan_chen 	aspeed_spi_stop_user(priv, flash);
613499853d6Sryan_chen 
614528cd552Sryan_chen 	debug("=== write opcode [%x] ==== \n", opcode);
615499853d6Sryan_chen 	switch(opcode) {
616499853d6Sryan_chen 		case SPINOR_OP_EN4B:
6170a73b911SChin-Ting Kuo 			/* For ast2600, if 2 chips ABR mode is enabled,
6180a73b911SChin-Ting Kuo 			 * turn on 3B mode auto clear in order to avoid
6190a73b911SChin-Ting Kuo 			 * the scenario where spi controller is at 4B mode
6200a73b911SChin-Ting Kuo 			 * and flash site is at 3B mode after 3rd switch.
6210a73b911SChin-Ting Kuo 			 */
6220a73b911SChin-Ting Kuo 			if (priv->new_ver == 1 && (readl(SPI_3B_AUTO_CLR_REG) & SPI_3B_AUTO_CLR))
6230a73b911SChin-Ting Kuo 				writel(readl(&priv->regs->soft_rst_cmd_ctrl) | SOFT_RST_CMD_EN,
6240a73b911SChin-Ting Kuo 						&priv->regs->soft_rst_cmd_ctrl);
6250a73b911SChin-Ting Kuo 
626edbd932bSChin-Ting Kuo 			writel(readl(&priv->regs->ctrl) | (0x11 << flash->cs), &priv->regs->ctrl);
627499853d6Sryan_chen 			break;
628499853d6Sryan_chen 		case SPINOR_OP_EX4B:
629edbd932bSChin-Ting Kuo 			writel(readl(&priv->regs->ctrl) & ~(0x11 << flash->cs), &priv->regs->ctrl);
630499853d6Sryan_chen 			break;
631499853d6Sryan_chen 	}
632499853d6Sryan_chen 	return 0;
633499853d6Sryan_chen }
634499853d6Sryan_chen 
635499853d6Sryan_chen static void aspeed_spi_send_cmd_addr(struct aspeed_spi_priv *priv,
636499853d6Sryan_chen 				     struct aspeed_spi_flash *flash,
637499853d6Sryan_chen 				     const u8 *cmdbuf, unsigned int cmdlen)
638499853d6Sryan_chen {
639499853d6Sryan_chen 	int i;
640499853d6Sryan_chen 	u8 byte0 = 0x0;
641499853d6Sryan_chen 	u8 addrlen = cmdlen - 1;
642499853d6Sryan_chen 
643499853d6Sryan_chen 	/* First, send the opcode */
644499853d6Sryan_chen 	aspeed_spi_write_to_ahb(flash->ahb_base, &cmdbuf[0], 1);
645499853d6Sryan_chen 
646528cd552Sryan_chen 	if(flash->iomode == CE_CTRL_IO_QUAD_ADDR_DATA)
647528cd552Sryan_chen 		writel(flash->ce_ctrl_user | flash->iomode, &priv->regs->ce_ctrl[flash->cs]);
648528cd552Sryan_chen 
649499853d6Sryan_chen 	/*
650499853d6Sryan_chen 	 * The controller is configured for 4BYTE address mode. Fix
651499853d6Sryan_chen 	 * the address width and send an extra byte if the SPI Flash
652499853d6Sryan_chen 	 * layer uses 3 bytes addresses.
653499853d6Sryan_chen 	 */
654499853d6Sryan_chen 	if (addrlen == 3 && readl(&priv->regs->ctrl) & BIT(flash->cs))
655499853d6Sryan_chen 		aspeed_spi_write_to_ahb(flash->ahb_base, &byte0, 1);
656499853d6Sryan_chen 
657499853d6Sryan_chen 	/* Then the address */
658499853d6Sryan_chen 	for (i = 1 ; i < cmdlen; i++)
659499853d6Sryan_chen 		aspeed_spi_write_to_ahb(flash->ahb_base, &cmdbuf[i], 1);
660499853d6Sryan_chen }
661499853d6Sryan_chen 
662499853d6Sryan_chen static ssize_t aspeed_spi_read_user(struct aspeed_spi_priv *priv,
663499853d6Sryan_chen 				    struct aspeed_spi_flash *flash,
664499853d6Sryan_chen 				    unsigned int cmdlen, const u8 *cmdbuf,
665499853d6Sryan_chen 				    unsigned int len, u8 *read_buf)
666499853d6Sryan_chen {
667499853d6Sryan_chen 	u8 dummy = 0xff;
668499853d6Sryan_chen 	int i;
669499853d6Sryan_chen 
670499853d6Sryan_chen 	aspeed_spi_start_user(priv, flash);
671499853d6Sryan_chen 
672499853d6Sryan_chen 	/* cmd buffer = cmd + addr + dummies */
673499853d6Sryan_chen 	aspeed_spi_send_cmd_addr(priv, flash, cmdbuf,
674499853d6Sryan_chen 				 cmdlen - (flash->spi->read_dummy/8));
675499853d6Sryan_chen 
676499853d6Sryan_chen 	for (i = 0 ; i < (flash->spi->read_dummy/8); i++)
677499853d6Sryan_chen 		aspeed_spi_write_to_ahb(flash->ahb_base, &dummy, 1);
678499853d6Sryan_chen 
679499853d6Sryan_chen 	if (flash->iomode) {
680499853d6Sryan_chen 		clrbits_le32(&priv->regs->ce_ctrl[flash->cs],
681499853d6Sryan_chen 			     CE_CTRL_IO_MODE_MASK);
682499853d6Sryan_chen 		setbits_le32(&priv->regs->ce_ctrl[flash->cs], flash->iomode);
683499853d6Sryan_chen 	}
684499853d6Sryan_chen 
685499853d6Sryan_chen 	aspeed_spi_read_from_ahb(flash->ahb_base, read_buf, len);
686499853d6Sryan_chen 	aspeed_spi_stop_user(priv, flash);
687499853d6Sryan_chen 
688499853d6Sryan_chen 	return 0;
689499853d6Sryan_chen }
690499853d6Sryan_chen 
691499853d6Sryan_chen static ssize_t aspeed_spi_write_user(struct aspeed_spi_priv *priv,
692499853d6Sryan_chen 				     struct aspeed_spi_flash *flash,
693499853d6Sryan_chen 				     unsigned int cmdlen, const u8 *cmdbuf,
694499853d6Sryan_chen 				     unsigned int len,	const u8 *write_buf)
695499853d6Sryan_chen {
696499853d6Sryan_chen 	aspeed_spi_start_user(priv, flash);
697499853d6Sryan_chen 
69876e3c7a9Sryan_chen 	/* cmd buffer = cmd + addr : normally cmd is use signle mode*/
699499853d6Sryan_chen 	aspeed_spi_send_cmd_addr(priv, flash, cmdbuf, cmdlen);
70076e3c7a9Sryan_chen 
70176e3c7a9Sryan_chen 	/* data will use io mode */
70276e3c7a9Sryan_chen 	if(flash->iomode == CE_CTRL_IO_QUAD_DATA)
70376e3c7a9Sryan_chen 		writel(flash->ce_ctrl_user | flash->iomode, &priv->regs->ce_ctrl[flash->cs]);
70476e3c7a9Sryan_chen 
705499853d6Sryan_chen 	aspeed_spi_write_to_ahb(flash->ahb_base, write_buf, len);
706499853d6Sryan_chen 
707499853d6Sryan_chen 	aspeed_spi_stop_user(priv, flash);
708499853d6Sryan_chen 
709499853d6Sryan_chen 	return 0;
710499853d6Sryan_chen }
711499853d6Sryan_chen 
712499853d6Sryan_chen static u32 aspeed_spi_flash_to_addr(struct aspeed_spi_flash *flash,
713499853d6Sryan_chen 				    const u8 *cmdbuf, unsigned int cmdlen)
714499853d6Sryan_chen {
715499853d6Sryan_chen 	u8 addrlen = cmdlen - 1;
716499853d6Sryan_chen 	u32 addr = (cmdbuf[1] << 16) | (cmdbuf[2] << 8) | cmdbuf[3];
717499853d6Sryan_chen 
718499853d6Sryan_chen 	/*
719499853d6Sryan_chen 	 * U-Boot SPI Flash layer uses 3 bytes addresses, but it might
720499853d6Sryan_chen 	 * change one day
721499853d6Sryan_chen 	 */
722499853d6Sryan_chen 	if (addrlen == 4)
723499853d6Sryan_chen 		addr = (addr << 8) | cmdbuf[4];
724499853d6Sryan_chen 
725499853d6Sryan_chen 	return addr;
726499853d6Sryan_chen }
727499853d6Sryan_chen 
728499853d6Sryan_chen /* TODO(clg@kaod.org): add support for XFER_MMAP instead ? */
729499853d6Sryan_chen static ssize_t aspeed_spi_read(struct aspeed_spi_priv *priv,
730499853d6Sryan_chen 			       struct aspeed_spi_flash *flash,
731499853d6Sryan_chen 			       unsigned int cmdlen, const u8 *cmdbuf,
732499853d6Sryan_chen 			       unsigned int len, u8 *read_buf)
733499853d6Sryan_chen {
734499853d6Sryan_chen 	/* cmd buffer = cmd + addr + dummies */
735499853d6Sryan_chen 	u32 offset = aspeed_spi_flash_to_addr(flash, cmdbuf,
736499853d6Sryan_chen 					      cmdlen - (flash->spi->read_dummy/8));
737499853d6Sryan_chen 
738499853d6Sryan_chen 	/*
7396424f9abSChin-Ting Kuo 	 * Switch to USER command mode:
7406424f9abSChin-Ting Kuo 	 * - if the AHB window configured for the device is
7416424f9abSChin-Ting Kuo 	 *   too small for the read operation
7426424f9abSChin-Ting Kuo 	 * - if read offset is smaller than the decoded start address
7436424f9abSChin-Ting Kuo 	 *   and the decoded range is not multiple of flash size
744499853d6Sryan_chen 	 */
7456424f9abSChin-Ting Kuo 	if ((offset + len >= flash->ahb_size) || \
7466424f9abSChin-Ting Kuo 		(offset < ((int)flash->ahb_base & 0x0FFFFFFF) && \
7476424f9abSChin-Ting Kuo 		(((int)flash->ahb_base & 0x0FFFFFFF) % flash->spi->size) != 0)) {
748499853d6Sryan_chen 		return aspeed_spi_read_user(priv, flash, cmdlen, cmdbuf,
749499853d6Sryan_chen 					    len, read_buf);
750499853d6Sryan_chen 	}
751499853d6Sryan_chen 
752499853d6Sryan_chen 	memcpy_fromio(read_buf, flash->ahb_base + offset, len);
753499853d6Sryan_chen 
754499853d6Sryan_chen 	return 0;
755499853d6Sryan_chen }
756499853d6Sryan_chen 
757499853d6Sryan_chen static int aspeed_spi_xfer(struct udevice *dev, unsigned int bitlen,
758499853d6Sryan_chen 			   const void *dout, void *din, unsigned long flags)
759499853d6Sryan_chen {
760499853d6Sryan_chen 	struct udevice *bus = dev->parent;
761499853d6Sryan_chen 	struct aspeed_spi_priv *priv = dev_get_priv(bus);
762499853d6Sryan_chen 	struct aspeed_spi_flash *flash;
763499853d6Sryan_chen 	u8 *cmd_buf = priv->cmd_buf;
764499853d6Sryan_chen 	size_t data_bytes;
765499853d6Sryan_chen 	int err = 0;
766499853d6Sryan_chen 
767499853d6Sryan_chen 	flash = aspeed_spi_get_flash(dev);
768499853d6Sryan_chen 	if (!flash)
769499853d6Sryan_chen 		return -ENXIO;
770499853d6Sryan_chen 
771499853d6Sryan_chen 	if (flags & SPI_XFER_BEGIN) {
772499853d6Sryan_chen 		/* save command in progress */
773499853d6Sryan_chen 		priv->cmd_len = bitlen / 8;
774499853d6Sryan_chen 		memcpy(cmd_buf, dout, priv->cmd_len);
775499853d6Sryan_chen 	}
776499853d6Sryan_chen 
777499853d6Sryan_chen 	if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
778499853d6Sryan_chen 		/* if start and end bit are set, the data bytes is 0. */
779499853d6Sryan_chen 		data_bytes = 0;
780499853d6Sryan_chen 	} else {
781499853d6Sryan_chen 		data_bytes = bitlen / 8;
782499853d6Sryan_chen 	}
783499853d6Sryan_chen 
784499853d6Sryan_chen 	debug("CS%u: %s cmd %zu bytes data %zu bytes\n", flash->cs,
785499853d6Sryan_chen 	      din ? "read" : "write", priv->cmd_len, data_bytes);
786499853d6Sryan_chen 
787499853d6Sryan_chen 	if ((flags & SPI_XFER_END) || flags == 0) {
788499853d6Sryan_chen 		if (priv->cmd_len == 0) {
789499853d6Sryan_chen 			pr_err("No command is progress !\n");
790499853d6Sryan_chen 			return -1;
791499853d6Sryan_chen 		}
792499853d6Sryan_chen 
793499853d6Sryan_chen 		if (din && data_bytes) {
794499853d6Sryan_chen 			if (priv->cmd_len == 1)
795499853d6Sryan_chen 				err = aspeed_spi_read_reg(priv, flash,
796499853d6Sryan_chen 							  cmd_buf[0],
797499853d6Sryan_chen 							  din, data_bytes);
798499853d6Sryan_chen 			else
799499853d6Sryan_chen 				err = aspeed_spi_read(priv, flash,
800499853d6Sryan_chen 						      priv->cmd_len,
801499853d6Sryan_chen 						      cmd_buf, data_bytes,
802499853d6Sryan_chen 						      din);
803499853d6Sryan_chen 		} else if (dout) {
804499853d6Sryan_chen 			if (priv->cmd_len == 1)
805499853d6Sryan_chen 				err = aspeed_spi_write_reg(priv, flash,
806499853d6Sryan_chen 							   cmd_buf[0],
807499853d6Sryan_chen 							   dout, data_bytes);
808499853d6Sryan_chen 			else
809499853d6Sryan_chen 				err = aspeed_spi_write_user(priv, flash,
810499853d6Sryan_chen 							    priv->cmd_len,
811499853d6Sryan_chen 							    cmd_buf, data_bytes,
812499853d6Sryan_chen 							    dout);
813499853d6Sryan_chen 		}
814499853d6Sryan_chen 
815499853d6Sryan_chen 		if (flags & SPI_XFER_END) {
816499853d6Sryan_chen 			/* clear command */
817499853d6Sryan_chen 			memset(cmd_buf, 0, sizeof(priv->cmd_buf));
818499853d6Sryan_chen 			priv->cmd_len = 0;
819499853d6Sryan_chen 		}
820499853d6Sryan_chen 	}
821499853d6Sryan_chen 
822499853d6Sryan_chen 	return err;
823499853d6Sryan_chen }
824499853d6Sryan_chen 
825499853d6Sryan_chen static int aspeed_spi_child_pre_probe(struct udevice *dev)
826499853d6Sryan_chen {
827499853d6Sryan_chen 	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
828499853d6Sryan_chen 
829499853d6Sryan_chen 	debug("pre_probe slave device on CS%u, max_hz %u, mode 0x%x.\n",
830499853d6Sryan_chen 	      slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
831499853d6Sryan_chen 
832499853d6Sryan_chen 	if (!aspeed_spi_get_flash(dev))
833499853d6Sryan_chen 		return -ENXIO;
834499853d6Sryan_chen 
835499853d6Sryan_chen 	return 0;
836499853d6Sryan_chen }
837499853d6Sryan_chen 
838499853d6Sryan_chen /*
839499853d6Sryan_chen  * It is possible to automatically define a contiguous address space
840499853d6Sryan_chen  * on top of all CEs in the AHB window of the controller but it would
841499853d6Sryan_chen  * require much more work. Let's start with a simple mapping scheme
842499853d6Sryan_chen  * which should work fine for a single flash device.
843499853d6Sryan_chen  *
844499853d6Sryan_chen  * More complex schemes should probably be defined with the device
845499853d6Sryan_chen  * tree.
846499853d6Sryan_chen  */
847499853d6Sryan_chen static int aspeed_spi_flash_set_segment(struct aspeed_spi_priv *priv,
848499853d6Sryan_chen 					struct aspeed_spi_flash *flash)
849499853d6Sryan_chen {
850499853d6Sryan_chen 	u32 seg_addr;
851499853d6Sryan_chen 
852499853d6Sryan_chen 	/* could be configured through the device tree */
853499853d6Sryan_chen 	flash->ahb_size = flash->spi->size;
854499853d6Sryan_chen 
855da83dd7eSryan_chen 	if (priv->new_ver) {
856da83dd7eSryan_chen 		seg_addr = G6_SEGMENT_ADDR_VALUE((u32)flash->ahb_base,
857da83dd7eSryan_chen 					      (u32)flash->ahb_base + flash->ahb_size);
858da83dd7eSryan_chen 	} else {
859499853d6Sryan_chen 		seg_addr = SEGMENT_ADDR_VALUE((u32)flash->ahb_base,
860499853d6Sryan_chen 						  (u32)flash->ahb_base + flash->ahb_size);
861da83dd7eSryan_chen 	}
862499853d6Sryan_chen 	writel(seg_addr, &priv->regs->segment_addr[flash->cs]);
863499853d6Sryan_chen 
864499853d6Sryan_chen 	return 0;
865499853d6Sryan_chen }
866499853d6Sryan_chen 
867499853d6Sryan_chen static int aspeed_spi_flash_init(struct aspeed_spi_priv *priv,
868499853d6Sryan_chen 				 struct aspeed_spi_flash *flash,
869499853d6Sryan_chen 				 struct udevice *dev)
870499853d6Sryan_chen {
871beec505fSChin-Ting Kuo 	int ret;
872499853d6Sryan_chen 	struct spi_flash *spi_flash = dev_get_uclass_priv(dev);
873499853d6Sryan_chen 	struct spi_slave *slave = dev_get_parent_priv(dev);
874499853d6Sryan_chen 	u32 read_hclk;
875499853d6Sryan_chen 
876beec505fSChin-Ting Kuo 
877499853d6Sryan_chen 	/*
878499853d6Sryan_chen 	 * The SPI flash device slave should not change, so initialize
879499853d6Sryan_chen 	 * it only once.
880499853d6Sryan_chen 	 */
881499853d6Sryan_chen 	if (flash->init)
882499853d6Sryan_chen 		return 0;
883499853d6Sryan_chen 
884499853d6Sryan_chen 	/*
885499853d6Sryan_chen 	 * The flash device has not been probed yet. Initial transfers
886499853d6Sryan_chen 	 * to read the JEDEC of the device will use the initial
887499853d6Sryan_chen 	 * default settings of the registers.
888499853d6Sryan_chen 	 */
889499853d6Sryan_chen 	if (!spi_flash->name)
890499853d6Sryan_chen 		return 0;
891499853d6Sryan_chen 
892499853d6Sryan_chen 	debug("CS%u: init %s flags:%x size:%d page:%d sector:%d erase:%d "
893499853d6Sryan_chen 	      "cmds [ erase:%x read=%x write:%x ] dummy:%d\n",
894499853d6Sryan_chen 	      flash->cs,
895499853d6Sryan_chen 	      spi_flash->name, spi_flash->flags, spi_flash->size,
896499853d6Sryan_chen 	      spi_flash->page_size, spi_flash->sector_size,
897499853d6Sryan_chen 	      spi_flash->erase_size, spi_flash->erase_opcode,
898499853d6Sryan_chen 	      spi_flash->read_opcode, spi_flash->program_opcode,
899499853d6Sryan_chen 	      spi_flash->read_dummy);
900499853d6Sryan_chen 
901499853d6Sryan_chen 	flash->spi = spi_flash;
902499853d6Sryan_chen 
9037d182336Sryan_chen 	flash->ce_ctrl_user = CE_CTRL_USERMODE;
904499853d6Sryan_chen 
905ac86fa8bSryan_chen 	if(priv->new_ver)
906ac86fa8bSryan_chen 		read_hclk = aspeed_g6_spi_hclk_divisor(priv, slave->speed);
907ac86fa8bSryan_chen 	else
9087d182336Sryan_chen 		read_hclk = aspeed_spi_hclk_divisor(priv, slave->speed);
909499853d6Sryan_chen 
910528cd552Sryan_chen 	switch(flash->spi->read_opcode) {
911528cd552Sryan_chen 		case SPINOR_OP_READ_1_1_2:
912528cd552Sryan_chen 		case SPINOR_OP_READ_1_1_2_4B:
913499853d6Sryan_chen 			flash->iomode = CE_CTRL_IO_DUAL_DATA;
914528cd552Sryan_chen 			break;
915528cd552Sryan_chen 		case SPINOR_OP_READ_1_1_4:
916528cd552Sryan_chen 		case SPINOR_OP_READ_1_1_4_4B:
917d32338fdSryan_chen 			flash->iomode = CE_CTRL_IO_QUAD_DATA;
918528cd552Sryan_chen 			break;
919528cd552Sryan_chen 		case SPINOR_OP_READ_1_4_4:
920528cd552Sryan_chen 		case SPINOR_OP_READ_1_4_4_4B:
921528cd552Sryan_chen 			flash->iomode = CE_CTRL_IO_QUAD_ADDR_DATA;
922528cd552Sryan_chen 			printf("need modify dummy for 3 bytes");
923528cd552Sryan_chen 			break;
924499853d6Sryan_chen 	}
925499853d6Sryan_chen 
926d32338fdSryan_chen 	if(priv->new_ver) {
9277d182336Sryan_chen 		flash->ce_ctrl_fread = CE_G6_CTRL_CLOCK_FREQ(read_hclk) |
9287d182336Sryan_chen 			flash->iomode |
9297d182336Sryan_chen 			CE_CTRL_CMD(flash->spi->read_opcode) |
9307d182336Sryan_chen 			CE_CTRL_DUMMY((flash->spi->read_dummy/8)) |
9317d182336Sryan_chen 			CE_CTRL_FREADMODE;
932d32338fdSryan_chen 	} else {
933499853d6Sryan_chen 		flash->ce_ctrl_fread = CE_CTRL_CLOCK_FREQ(read_hclk) |
934499853d6Sryan_chen 			flash->iomode |
935499853d6Sryan_chen 			CE_CTRL_CMD(flash->spi->read_opcode) |
936499853d6Sryan_chen 			CE_CTRL_DUMMY((flash->spi->read_dummy/8)) |
937499853d6Sryan_chen 			CE_CTRL_FREADMODE;
938d32338fdSryan_chen 	}
939499853d6Sryan_chen 
9409405f2a1SChin-Ting Kuo 	if (flash->spi->addr_width == 4)
9419405f2a1SChin-Ting Kuo 		writel(readl(&priv->regs->ctrl) | 0x11 << flash->cs, &priv->regs->ctrl);
9429405f2a1SChin-Ting Kuo 
943499853d6Sryan_chen 	debug("CS%u: USER mode 0x%08x FREAD mode 0x%08x\n", flash->cs,
944499853d6Sryan_chen 	      flash->ce_ctrl_user, flash->ce_ctrl_fread);
945499853d6Sryan_chen 
946499853d6Sryan_chen 	/* Set the CE Control Register default (FAST READ) */
947499853d6Sryan_chen 	writel(flash->ce_ctrl_fread, &priv->regs->ce_ctrl[flash->cs]);
948499853d6Sryan_chen 
949499853d6Sryan_chen 	/* Set Address Segment Register for direct AHB accesses */
950499853d6Sryan_chen 	aspeed_spi_flash_set_segment(priv, flash);
951499853d6Sryan_chen 
952beec505fSChin-Ting Kuo 	/*
953beec505fSChin-Ting Kuo 	 * Set the Read Timing Compensation Register. This setting
954beec505fSChin-Ting Kuo 	 * applies to all devices.
955beec505fSChin-Ting Kuo 	 */
956beec505fSChin-Ting Kuo 	ret = aspeed_spi_timing_calibration(priv);
957beec505fSChin-Ting Kuo 	if (ret != 0)
958beec505fSChin-Ting Kuo 		return ret;
959beec505fSChin-Ting Kuo 
960499853d6Sryan_chen 	/* All done */
961499853d6Sryan_chen 	flash->init = true;
962499853d6Sryan_chen 
963499853d6Sryan_chen 	return 0;
964499853d6Sryan_chen }
965499853d6Sryan_chen 
966499853d6Sryan_chen static int aspeed_spi_claim_bus(struct udevice *dev)
967499853d6Sryan_chen {
968499853d6Sryan_chen 	struct udevice *bus = dev->parent;
969499853d6Sryan_chen 	struct aspeed_spi_priv *priv = dev_get_priv(bus);
970499853d6Sryan_chen 	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
971499853d6Sryan_chen 	struct aspeed_spi_flash *flash;
972499853d6Sryan_chen 
973499853d6Sryan_chen 	debug("%s: claim bus CS%u\n", bus->name, slave_plat->cs);
974499853d6Sryan_chen 
975499853d6Sryan_chen 	flash = aspeed_spi_get_flash(dev);
976499853d6Sryan_chen 	if (!flash)
977499853d6Sryan_chen 		return -ENODEV;
978499853d6Sryan_chen 
979499853d6Sryan_chen 	return aspeed_spi_flash_init(priv, flash, dev);
980499853d6Sryan_chen }
981499853d6Sryan_chen 
982499853d6Sryan_chen static int aspeed_spi_release_bus(struct udevice *dev)
983499853d6Sryan_chen {
984499853d6Sryan_chen 	struct udevice *bus = dev->parent;
985499853d6Sryan_chen 	struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
986499853d6Sryan_chen 
987499853d6Sryan_chen 	debug("%s: release bus CS%u\n", bus->name, slave_plat->cs);
988499853d6Sryan_chen 
989499853d6Sryan_chen 	if (!aspeed_spi_get_flash(dev))
990499853d6Sryan_chen 		return -ENODEV;
991499853d6Sryan_chen 
992499853d6Sryan_chen 	return 0;
993499853d6Sryan_chen }
994499853d6Sryan_chen 
995499853d6Sryan_chen static int aspeed_spi_set_mode(struct udevice *bus, uint mode)
996499853d6Sryan_chen {
997499853d6Sryan_chen 	debug("%s: setting mode to %x\n", bus->name, mode);
998499853d6Sryan_chen 
999499853d6Sryan_chen 	if (mode & (SPI_RX_QUAD | SPI_TX_QUAD)) {
100076e3c7a9Sryan_chen #ifndef CONFIG_ASPEED_AST2600
1001499853d6Sryan_chen 		pr_err("%s invalid QUAD IO mode\n", bus->name);
1002499853d6Sryan_chen 		return -EINVAL;
100376e3c7a9Sryan_chen #endif
1004499853d6Sryan_chen 	}
1005499853d6Sryan_chen 
1006499853d6Sryan_chen 	/* The CE Control Register is set in claim_bus() */
1007499853d6Sryan_chen 	return 0;
1008499853d6Sryan_chen }
1009499853d6Sryan_chen 
1010499853d6Sryan_chen static int aspeed_spi_set_speed(struct udevice *bus, uint hz)
1011499853d6Sryan_chen {
1012499853d6Sryan_chen 	debug("%s: setting speed to %u\n", bus->name, hz);
1013499853d6Sryan_chen 
1014499853d6Sryan_chen 	/* The CE Control Register is set in claim_bus() */
1015499853d6Sryan_chen 	return 0;
1016499853d6Sryan_chen }
1017499853d6Sryan_chen 
1018499853d6Sryan_chen static int aspeed_spi_count_flash_devices(struct udevice *bus)
1019499853d6Sryan_chen {
1020499853d6Sryan_chen 	ofnode node;
1021499853d6Sryan_chen 	int count = 0;
1022499853d6Sryan_chen 
1023499853d6Sryan_chen 	dev_for_each_subnode(node, bus) {
1024499853d6Sryan_chen 		if (ofnode_is_available(node) &&
1025499853d6Sryan_chen 		    ofnode_device_is_compatible(node, "spi-flash"))
1026499853d6Sryan_chen 			count++;
1027499853d6Sryan_chen 	}
1028499853d6Sryan_chen 
1029499853d6Sryan_chen 	return count;
1030499853d6Sryan_chen }
1031499853d6Sryan_chen 
1032499853d6Sryan_chen static int aspeed_spi_bind(struct udevice *bus)
1033499853d6Sryan_chen {
1034499853d6Sryan_chen 	debug("%s assigned req_seq=%d seq=%d\n", bus->name, bus->req_seq,
1035499853d6Sryan_chen 	      bus->seq);
1036499853d6Sryan_chen 
1037499853d6Sryan_chen 	return 0;
1038499853d6Sryan_chen }
1039499853d6Sryan_chen 
1040499853d6Sryan_chen static int aspeed_spi_probe(struct udevice *bus)
1041499853d6Sryan_chen {
1042499853d6Sryan_chen 	struct resource res_regs, res_ahb;
1043499853d6Sryan_chen 	struct aspeed_spi_priv *priv = dev_get_priv(bus);
1044499853d6Sryan_chen 	struct clk hclk;
1045499853d6Sryan_chen 	int ret;
1046499853d6Sryan_chen 
1047499853d6Sryan_chen 	ret = dev_read_resource(bus, 0, &res_regs);
1048499853d6Sryan_chen 	if (ret < 0)
1049499853d6Sryan_chen 		return ret;
1050499853d6Sryan_chen 
1051499853d6Sryan_chen 	priv->regs = (void __iomem *)res_regs.start;
1052499853d6Sryan_chen 
1053499853d6Sryan_chen 	ret = dev_read_resource(bus, 1, &res_ahb);
1054499853d6Sryan_chen 	if (ret < 0)
1055499853d6Sryan_chen 		return ret;
1056499853d6Sryan_chen 
1057499853d6Sryan_chen 	priv->ahb_base = (void __iomem *)res_ahb.start;
1058499853d6Sryan_chen 	priv->ahb_size = res_ahb.end - res_ahb.start;
1059499853d6Sryan_chen 
1060499853d6Sryan_chen 	ret = clk_get_by_index(bus, 0, &hclk);
1061499853d6Sryan_chen 	if (ret < 0) {
1062499853d6Sryan_chen 		pr_err("%s could not get clock: %d\n", bus->name, ret);
1063499853d6Sryan_chen 		return ret;
1064499853d6Sryan_chen 	}
1065499853d6Sryan_chen 
1066499853d6Sryan_chen 	priv->hclk_rate = clk_get_rate(&hclk);
1067499853d6Sryan_chen 	clk_free(&hclk);
1068499853d6Sryan_chen 
1069499853d6Sryan_chen 	priv->max_hz = dev_read_u32_default(bus, "spi-max-frequency",
1070499853d6Sryan_chen 					    100000000);
1071499853d6Sryan_chen 
1072499853d6Sryan_chen 	priv->num_cs = dev_read_u32_default(bus, "num-cs", ASPEED_SPI_MAX_CS);
1073499853d6Sryan_chen 
1074499853d6Sryan_chen 	priv->flash_count = aspeed_spi_count_flash_devices(bus);
1075499853d6Sryan_chen 	if (priv->flash_count > priv->num_cs) {
1076499853d6Sryan_chen 		pr_err("%s has too many flash devices: %d\n", bus->name,
1077499853d6Sryan_chen 		       priv->flash_count);
1078499853d6Sryan_chen 		return -EINVAL;
1079499853d6Sryan_chen 	}
1080499853d6Sryan_chen 
1081499853d6Sryan_chen 	if (!priv->flash_count) {
1082499853d6Sryan_chen 		pr_err("%s has no flash devices ?!\n", bus->name);
1083499853d6Sryan_chen 		return -ENODEV;
1084499853d6Sryan_chen 	}
1085499853d6Sryan_chen 
10867d182336Sryan_chen 	if (device_is_compatible(bus, "aspeed,ast2600-fmc") ||
1087f87fadc3Sryan_chen 			device_is_compatible(bus, "aspeed,ast2600-spi")) {
1088499853d6Sryan_chen 		priv->new_ver = 1;
1089499853d6Sryan_chen 	}
1090499853d6Sryan_chen 
1091499853d6Sryan_chen 	/*
1092499853d6Sryan_chen 	 * There are some slight differences between the FMC and the
1093499853d6Sryan_chen 	 * SPI controllers
1094499853d6Sryan_chen 	 */
1095499853d6Sryan_chen 	priv->is_fmc = dev_get_driver_data(bus);
1096499853d6Sryan_chen 
1097499853d6Sryan_chen 	ret = aspeed_spi_controller_init(priv);
1098499853d6Sryan_chen 	if (ret)
1099499853d6Sryan_chen 		return ret;
1100499853d6Sryan_chen 
1101499853d6Sryan_chen 	debug("%s probed regs=%p ahb_base=%p max-hz=%d cs=%d seq=%d\n",
1102499853d6Sryan_chen 	      bus->name, priv->regs, priv->ahb_base, priv->max_hz,
1103499853d6Sryan_chen 	      priv->flash_count, bus->seq);
1104499853d6Sryan_chen 
1105499853d6Sryan_chen 	return 0;
1106499853d6Sryan_chen }
1107499853d6Sryan_chen 
1108499853d6Sryan_chen static const struct dm_spi_ops aspeed_spi_ops = {
1109499853d6Sryan_chen 	.claim_bus	= aspeed_spi_claim_bus,
1110499853d6Sryan_chen 	.release_bus	= aspeed_spi_release_bus,
1111499853d6Sryan_chen 	.set_mode	= aspeed_spi_set_mode,
1112499853d6Sryan_chen 	.set_speed	= aspeed_spi_set_speed,
1113499853d6Sryan_chen 	.xfer		= aspeed_spi_xfer,
1114499853d6Sryan_chen };
1115499853d6Sryan_chen 
1116499853d6Sryan_chen static const struct udevice_id aspeed_spi_ids[] = {
1117499853d6Sryan_chen 	{ .compatible = "aspeed,ast2600-fmc", .data = 1 },
1118499853d6Sryan_chen 	{ .compatible = "aspeed,ast2600-spi", .data = 0 },
1119499853d6Sryan_chen 	{ .compatible = "aspeed,ast2500-fmc", .data = 1 },
1120499853d6Sryan_chen 	{ .compatible = "aspeed,ast2500-spi", .data = 0 },
1121499853d6Sryan_chen 	{ }
1122499853d6Sryan_chen };
1123499853d6Sryan_chen 
1124499853d6Sryan_chen U_BOOT_DRIVER(aspeed_spi) = {
1125499853d6Sryan_chen 	.name = "aspeed_spi",
1126499853d6Sryan_chen 	.id = UCLASS_SPI,
1127499853d6Sryan_chen 	.of_match = aspeed_spi_ids,
1128499853d6Sryan_chen 	.ops = &aspeed_spi_ops,
1129499853d6Sryan_chen 	.priv_auto_alloc_size = sizeof(struct aspeed_spi_priv),
1130499853d6Sryan_chen 	.child_pre_probe = aspeed_spi_child_pre_probe,
1131499853d6Sryan_chen 	.bind  = aspeed_spi_bind,
1132499853d6Sryan_chen 	.probe = aspeed_spi_probe,
1133499853d6Sryan_chen };
1134