xref: /OK3568_Linux_fs/u-boot/drivers/spi/ich.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011-12 The Chromium OS Authors.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This file is derived from the flashrom project.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <errno.h>
12*4882a593Smuzhiyun #include <malloc.h>
13*4882a593Smuzhiyun #include <pch.h>
14*4882a593Smuzhiyun #include <pci.h>
15*4882a593Smuzhiyun #include <pci_ids.h>
16*4882a593Smuzhiyun #include <spi.h>
17*4882a593Smuzhiyun #include <asm/io.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "ich.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #ifdef DEBUG_TRACE
24*4882a593Smuzhiyun #define debug_trace(fmt, args...) debug(fmt, ##args)
25*4882a593Smuzhiyun #else
26*4882a593Smuzhiyun #define debug_trace(x, args...)
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 
ich_readb(struct ich_spi_priv * priv,int reg)29*4882a593Smuzhiyun static u8 ich_readb(struct ich_spi_priv *priv, int reg)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	u8 value = readb(priv->base + reg);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	debug_trace("read %2.2x from %4.4x\n", value, reg);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	return value;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
ich_readw(struct ich_spi_priv * priv,int reg)38*4882a593Smuzhiyun static u16 ich_readw(struct ich_spi_priv *priv, int reg)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	u16 value = readw(priv->base + reg);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	debug_trace("read %4.4x from %4.4x\n", value, reg);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return value;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
ich_readl(struct ich_spi_priv * priv,int reg)47*4882a593Smuzhiyun static u32 ich_readl(struct ich_spi_priv *priv, int reg)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	u32 value = readl(priv->base + reg);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	debug_trace("read %8.8x from %4.4x\n", value, reg);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	return value;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
ich_writeb(struct ich_spi_priv * priv,u8 value,int reg)56*4882a593Smuzhiyun static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	writeb(value, priv->base + reg);
59*4882a593Smuzhiyun 	debug_trace("wrote %2.2x to %4.4x\n", value, reg);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
ich_writew(struct ich_spi_priv * priv,u16 value,int reg)62*4882a593Smuzhiyun static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	writew(value, priv->base + reg);
65*4882a593Smuzhiyun 	debug_trace("wrote %4.4x to %4.4x\n", value, reg);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
ich_writel(struct ich_spi_priv * priv,u32 value,int reg)68*4882a593Smuzhiyun static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	writel(value, priv->base + reg);
71*4882a593Smuzhiyun 	debug_trace("wrote %8.8x to %4.4x\n", value, reg);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
write_reg(struct ich_spi_priv * priv,const void * value,int dest_reg,uint32_t size)74*4882a593Smuzhiyun static void write_reg(struct ich_spi_priv *priv, const void *value,
75*4882a593Smuzhiyun 		      int dest_reg, uint32_t size)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	memcpy_toio(priv->base + dest_reg, value, size);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
read_reg(struct ich_spi_priv * priv,int src_reg,void * value,uint32_t size)80*4882a593Smuzhiyun static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
81*4882a593Smuzhiyun 		     uint32_t size)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	memcpy_fromio(value, priv->base + src_reg, size);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
ich_set_bbar(struct ich_spi_priv * ctlr,uint32_t minaddr)86*4882a593Smuzhiyun static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	const uint32_t bbar_mask = 0x00ffff00;
89*4882a593Smuzhiyun 	uint32_t ichspi_bbar;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	minaddr &= bbar_mask;
92*4882a593Smuzhiyun 	ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
93*4882a593Smuzhiyun 	ichspi_bbar |= minaddr;
94*4882a593Smuzhiyun 	ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* @return 1 if the SPI flash supports the 33MHz speed */
ich9_can_do_33mhz(struct udevice * dev)98*4882a593Smuzhiyun static int ich9_can_do_33mhz(struct udevice *dev)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	u32 fdod, speed;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Observe SPI Descriptor Component Section 0 */
103*4882a593Smuzhiyun 	dm_pci_write_config32(dev->parent, 0xb0, 0x1000);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Extract the Write/Erase SPI Frequency from descriptor */
106*4882a593Smuzhiyun 	dm_pci_read_config32(dev->parent, 0xb4, &fdod);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
109*4882a593Smuzhiyun 	speed = (fdod >> 21) & 7;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	return speed == 1;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
ich_init_controller(struct udevice * dev,struct ich_spi_platdata * plat,struct ich_spi_priv * ctlr)114*4882a593Smuzhiyun static int ich_init_controller(struct udevice *dev,
115*4882a593Smuzhiyun 			       struct ich_spi_platdata *plat,
116*4882a593Smuzhiyun 			       struct ich_spi_priv *ctlr)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	ulong sbase_addr;
119*4882a593Smuzhiyun 	void *sbase;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* SBASE is similar */
122*4882a593Smuzhiyun 	pch_get_spi_base(dev->parent, &sbase_addr);
123*4882a593Smuzhiyun 	sbase = (void *)sbase_addr;
124*4882a593Smuzhiyun 	debug("%s: sbase=%p\n", __func__, sbase);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_7) {
127*4882a593Smuzhiyun 		struct ich7_spi_regs *ich7_spi = sbase;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
130*4882a593Smuzhiyun 		ctlr->menubytes = sizeof(ich7_spi->opmenu);
131*4882a593Smuzhiyun 		ctlr->optype = offsetof(struct ich7_spi_regs, optype);
132*4882a593Smuzhiyun 		ctlr->addr = offsetof(struct ich7_spi_regs, spia);
133*4882a593Smuzhiyun 		ctlr->data = offsetof(struct ich7_spi_regs, spid);
134*4882a593Smuzhiyun 		ctlr->databytes = sizeof(ich7_spi->spid);
135*4882a593Smuzhiyun 		ctlr->status = offsetof(struct ich7_spi_regs, spis);
136*4882a593Smuzhiyun 		ctlr->control = offsetof(struct ich7_spi_regs, spic);
137*4882a593Smuzhiyun 		ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
138*4882a593Smuzhiyun 		ctlr->preop = offsetof(struct ich7_spi_regs, preop);
139*4882a593Smuzhiyun 		ctlr->base = ich7_spi;
140*4882a593Smuzhiyun 	} else if (plat->ich_version == ICHV_9) {
141*4882a593Smuzhiyun 		struct ich9_spi_regs *ich9_spi = sbase;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
144*4882a593Smuzhiyun 		ctlr->menubytes = sizeof(ich9_spi->opmenu);
145*4882a593Smuzhiyun 		ctlr->optype = offsetof(struct ich9_spi_regs, optype);
146*4882a593Smuzhiyun 		ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
147*4882a593Smuzhiyun 		ctlr->data = offsetof(struct ich9_spi_regs, fdata);
148*4882a593Smuzhiyun 		ctlr->databytes = sizeof(ich9_spi->fdata);
149*4882a593Smuzhiyun 		ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
150*4882a593Smuzhiyun 		ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
151*4882a593Smuzhiyun 		ctlr->speed = ctlr->control + 2;
152*4882a593Smuzhiyun 		ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
153*4882a593Smuzhiyun 		ctlr->preop = offsetof(struct ich9_spi_regs, preop);
154*4882a593Smuzhiyun 		ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
155*4882a593Smuzhiyun 		ctlr->pr = &ich9_spi->pr[0];
156*4882a593Smuzhiyun 		ctlr->base = ich9_spi;
157*4882a593Smuzhiyun 	} else {
158*4882a593Smuzhiyun 		debug("ICH SPI: Unrecognised ICH version %d\n",
159*4882a593Smuzhiyun 		      plat->ich_version);
160*4882a593Smuzhiyun 		return -EINVAL;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Work out the maximum speed we can support */
164*4882a593Smuzhiyun 	ctlr->max_speed = 20000000;
165*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
166*4882a593Smuzhiyun 		ctlr->max_speed = 33000000;
167*4882a593Smuzhiyun 	debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
168*4882a593Smuzhiyun 	      plat->ich_version, ctlr->base, ctlr->max_speed);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	ich_set_bbar(ctlr, 0);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
spi_use_out(struct spi_trans * trans,unsigned bytes)175*4882a593Smuzhiyun static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	trans->out += bytes;
178*4882a593Smuzhiyun 	trans->bytesout -= bytes;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
spi_use_in(struct spi_trans * trans,unsigned bytes)181*4882a593Smuzhiyun static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	trans->in += bytes;
184*4882a593Smuzhiyun 	trans->bytesin -= bytes;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
spi_lock_down(struct ich_spi_platdata * plat,void * sbase)187*4882a593Smuzhiyun static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_7) {
190*4882a593Smuzhiyun 		struct ich7_spi_regs *ich7_spi = sbase;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 		setbits_le16(&ich7_spi->spis, SPIS_LOCK);
193*4882a593Smuzhiyun 	} else if (plat->ich_version == ICHV_9) {
194*4882a593Smuzhiyun 		struct ich9_spi_regs *ich9_spi = sbase;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
spi_lock_status(struct ich_spi_platdata * plat,void * sbase)200*4882a593Smuzhiyun static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	int lock = 0;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_7) {
205*4882a593Smuzhiyun 		struct ich7_spi_regs *ich7_spi = sbase;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		lock = readw(&ich7_spi->spis) & SPIS_LOCK;
208*4882a593Smuzhiyun 	} else if (plat->ich_version == ICHV_9) {
209*4882a593Smuzhiyun 		struct ich9_spi_regs *ich9_spi = sbase;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 		lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
212*4882a593Smuzhiyun 	}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	return lock != 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
spi_setup_type(struct spi_trans * trans,int data_bytes)217*4882a593Smuzhiyun static void spi_setup_type(struct spi_trans *trans, int data_bytes)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	trans->type = 0xFF;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* Try to guess spi type from read/write sizes */
222*4882a593Smuzhiyun 	if (trans->bytesin == 0) {
223*4882a593Smuzhiyun 		if (trans->bytesout + data_bytes > 4)
224*4882a593Smuzhiyun 			/*
225*4882a593Smuzhiyun 			 * If bytesin = 0 and bytesout > 4, we presume this is
226*4882a593Smuzhiyun 			 * a write data operation, which is accompanied by an
227*4882a593Smuzhiyun 			 * address.
228*4882a593Smuzhiyun 			 */
229*4882a593Smuzhiyun 			trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
230*4882a593Smuzhiyun 		else
231*4882a593Smuzhiyun 			trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
232*4882a593Smuzhiyun 		return;
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (trans->bytesout == 1) {	/* and bytesin is > 0 */
236*4882a593Smuzhiyun 		trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
237*4882a593Smuzhiyun 		return;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (trans->bytesout == 4)	/* and bytesin is > 0 */
241*4882a593Smuzhiyun 		trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/* Fast read command is called with 5 bytes instead of 4 */
244*4882a593Smuzhiyun 	if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
245*4882a593Smuzhiyun 		trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
246*4882a593Smuzhiyun 		--trans->bytesout;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
spi_setup_opcode(struct ich_spi_priv * ctlr,struct spi_trans * trans,bool lock)250*4882a593Smuzhiyun static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
251*4882a593Smuzhiyun 			    bool lock)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	uint16_t optypes;
254*4882a593Smuzhiyun 	uint8_t opmenu[ctlr->menubytes];
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	trans->opcode = trans->out[0];
257*4882a593Smuzhiyun 	spi_use_out(trans, 1);
258*4882a593Smuzhiyun 	if (!lock) {
259*4882a593Smuzhiyun 		/* The lock is off, so just use index 0. */
260*4882a593Smuzhiyun 		ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
261*4882a593Smuzhiyun 		optypes = ich_readw(ctlr, ctlr->optype);
262*4882a593Smuzhiyun 		optypes = (optypes & 0xfffc) | (trans->type & 0x3);
263*4882a593Smuzhiyun 		ich_writew(ctlr, optypes, ctlr->optype);
264*4882a593Smuzhiyun 		return 0;
265*4882a593Smuzhiyun 	} else {
266*4882a593Smuzhiyun 		/* The lock is on. See if what we need is on the menu. */
267*4882a593Smuzhiyun 		uint8_t optype;
268*4882a593Smuzhiyun 		uint16_t opcode_index;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 		/* Write Enable is handled as atomic prefix */
271*4882a593Smuzhiyun 		if (trans->opcode == SPI_OPCODE_WREN)
272*4882a593Smuzhiyun 			return 0;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
275*4882a593Smuzhiyun 		for (opcode_index = 0; opcode_index < ctlr->menubytes;
276*4882a593Smuzhiyun 				opcode_index++) {
277*4882a593Smuzhiyun 			if (opmenu[opcode_index] == trans->opcode)
278*4882a593Smuzhiyun 				break;
279*4882a593Smuzhiyun 		}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 		if (opcode_index == ctlr->menubytes) {
282*4882a593Smuzhiyun 			printf("ICH SPI: Opcode %x not found\n",
283*4882a593Smuzhiyun 			       trans->opcode);
284*4882a593Smuzhiyun 			return -EINVAL;
285*4882a593Smuzhiyun 		}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 		optypes = ich_readw(ctlr, ctlr->optype);
288*4882a593Smuzhiyun 		optype = (optypes >> (opcode_index * 2)) & 0x3;
289*4882a593Smuzhiyun 		if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
290*4882a593Smuzhiyun 		    optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
291*4882a593Smuzhiyun 		    trans->bytesout >= 3) {
292*4882a593Smuzhiyun 			/* We guessed wrong earlier. Fix it up. */
293*4882a593Smuzhiyun 			trans->type = optype;
294*4882a593Smuzhiyun 		}
295*4882a593Smuzhiyun 		if (optype != trans->type) {
296*4882a593Smuzhiyun 			printf("ICH SPI: Transaction doesn't fit type %d\n",
297*4882a593Smuzhiyun 			       optype);
298*4882a593Smuzhiyun 			return -ENOSPC;
299*4882a593Smuzhiyun 		}
300*4882a593Smuzhiyun 		return opcode_index;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
spi_setup_offset(struct spi_trans * trans)304*4882a593Smuzhiyun static int spi_setup_offset(struct spi_trans *trans)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	/* Separate the SPI address and data */
307*4882a593Smuzhiyun 	switch (trans->type) {
308*4882a593Smuzhiyun 	case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
309*4882a593Smuzhiyun 	case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
310*4882a593Smuzhiyun 		return 0;
311*4882a593Smuzhiyun 	case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
312*4882a593Smuzhiyun 	case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
313*4882a593Smuzhiyun 		trans->offset = ((uint32_t)trans->out[0] << 16) |
314*4882a593Smuzhiyun 				((uint32_t)trans->out[1] << 8) |
315*4882a593Smuzhiyun 				((uint32_t)trans->out[2] << 0);
316*4882a593Smuzhiyun 		spi_use_out(trans, 3);
317*4882a593Smuzhiyun 		return 1;
318*4882a593Smuzhiyun 	default:
319*4882a593Smuzhiyun 		printf("Unrecognized SPI transaction type %#x\n", trans->type);
320*4882a593Smuzhiyun 		return -EPROTO;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun  * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
326*4882a593Smuzhiyun  * below is true) or 0. In case the wait was for the bit(s) to set - write
327*4882a593Smuzhiyun  * those bits back, which would cause resetting them.
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * Return the last read status value on success or -1 on failure.
330*4882a593Smuzhiyun  */
ich_status_poll(struct ich_spi_priv * ctlr,u16 bitmask,int wait_til_set)331*4882a593Smuzhiyun static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
332*4882a593Smuzhiyun 			   int wait_til_set)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	int timeout = 600000; /* This will result in 6s */
335*4882a593Smuzhiyun 	u16 status = 0;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	while (timeout--) {
338*4882a593Smuzhiyun 		status = ich_readw(ctlr, ctlr->status);
339*4882a593Smuzhiyun 		if (wait_til_set ^ ((status & bitmask) == 0)) {
340*4882a593Smuzhiyun 			if (wait_til_set) {
341*4882a593Smuzhiyun 				ich_writew(ctlr, status & bitmask,
342*4882a593Smuzhiyun 					   ctlr->status);
343*4882a593Smuzhiyun 			}
344*4882a593Smuzhiyun 			return status;
345*4882a593Smuzhiyun 		}
346*4882a593Smuzhiyun 		udelay(10);
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
350*4882a593Smuzhiyun 	       status, bitmask);
351*4882a593Smuzhiyun 	return -ETIMEDOUT;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
ich_spi_config_opcode(struct udevice * dev)354*4882a593Smuzhiyun void ich_spi_config_opcode(struct udevice *dev)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	struct ich_spi_priv *ctlr = dev_get_priv(dev);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/*
359*4882a593Smuzhiyun 	 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
360*4882a593Smuzhiyun 	 * to prevent accidental or intentional writes. Before they get
361*4882a593Smuzhiyun 	 * locked down, these registers should be initialized properly.
362*4882a593Smuzhiyun 	 */
363*4882a593Smuzhiyun 	ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
364*4882a593Smuzhiyun 	ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
365*4882a593Smuzhiyun 	ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
366*4882a593Smuzhiyun 	ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
ich_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)369*4882a593Smuzhiyun static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
370*4882a593Smuzhiyun 			const void *dout, void *din, unsigned long flags)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	struct udevice *bus = dev_get_parent(dev);
373*4882a593Smuzhiyun 	struct ich_spi_platdata *plat = dev_get_platdata(bus);
374*4882a593Smuzhiyun 	struct ich_spi_priv *ctlr = dev_get_priv(bus);
375*4882a593Smuzhiyun 	uint16_t control;
376*4882a593Smuzhiyun 	int16_t opcode_index;
377*4882a593Smuzhiyun 	int with_address;
378*4882a593Smuzhiyun 	int status;
379*4882a593Smuzhiyun 	int bytes = bitlen / 8;
380*4882a593Smuzhiyun 	struct spi_trans *trans = &ctlr->trans;
381*4882a593Smuzhiyun 	unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
382*4882a593Smuzhiyun 	int using_cmd = 0;
383*4882a593Smuzhiyun 	bool lock = spi_lock_status(plat, ctlr->base);
384*4882a593Smuzhiyun 	int ret;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	/* We don't support writing partial bytes */
387*4882a593Smuzhiyun 	if (bitlen % 8) {
388*4882a593Smuzhiyun 		debug("ICH SPI: Accessing partial bytes not supported\n");
389*4882a593Smuzhiyun 		return -EPROTONOSUPPORT;
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	/* An empty end transaction can be ignored */
393*4882a593Smuzhiyun 	if (type == SPI_XFER_END && !dout && !din)
394*4882a593Smuzhiyun 		return 0;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (type & SPI_XFER_BEGIN)
397*4882a593Smuzhiyun 		memset(trans, '\0', sizeof(*trans));
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* Dp we need to come back later to finish it? */
400*4882a593Smuzhiyun 	if (dout && type == SPI_XFER_BEGIN) {
401*4882a593Smuzhiyun 		if (bytes > ICH_MAX_CMD_LEN) {
402*4882a593Smuzhiyun 			debug("ICH SPI: Command length limit exceeded\n");
403*4882a593Smuzhiyun 			return -ENOSPC;
404*4882a593Smuzhiyun 		}
405*4882a593Smuzhiyun 		memcpy(trans->cmd, dout, bytes);
406*4882a593Smuzhiyun 		trans->cmd_len = bytes;
407*4882a593Smuzhiyun 		debug_trace("ICH SPI: Saved %d bytes\n", bytes);
408*4882a593Smuzhiyun 		return 0;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/*
412*4882a593Smuzhiyun 	 * We process a 'middle' spi_xfer() call, which has no
413*4882a593Smuzhiyun 	 * SPI_XFER_BEGIN/END, as an independent transaction as if it had
414*4882a593Smuzhiyun 	 * an end. We therefore repeat the command. This is because ICH
415*4882a593Smuzhiyun 	 * seems to have no support for this, or because interest (in digging
416*4882a593Smuzhiyun 	 * out the details and creating a special case in the code) is low.
417*4882a593Smuzhiyun 	 */
418*4882a593Smuzhiyun 	if (trans->cmd_len) {
419*4882a593Smuzhiyun 		trans->out = trans->cmd;
420*4882a593Smuzhiyun 		trans->bytesout = trans->cmd_len;
421*4882a593Smuzhiyun 		using_cmd = 1;
422*4882a593Smuzhiyun 		debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
423*4882a593Smuzhiyun 	} else {
424*4882a593Smuzhiyun 		trans->out = dout;
425*4882a593Smuzhiyun 		trans->bytesout = dout ? bytes : 0;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	trans->in = din;
429*4882a593Smuzhiyun 	trans->bytesin = din ? bytes : 0;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	/* There has to always at least be an opcode */
432*4882a593Smuzhiyun 	if (!trans->bytesout) {
433*4882a593Smuzhiyun 		debug("ICH SPI: No opcode for transfer\n");
434*4882a593Smuzhiyun 		return -EPROTO;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
438*4882a593Smuzhiyun 	if (ret < 0)
439*4882a593Smuzhiyun 		return ret;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_7)
442*4882a593Smuzhiyun 		ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
443*4882a593Smuzhiyun 	else
444*4882a593Smuzhiyun 		ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	spi_setup_type(trans, using_cmd ? bytes : 0);
447*4882a593Smuzhiyun 	opcode_index = spi_setup_opcode(ctlr, trans, lock);
448*4882a593Smuzhiyun 	if (opcode_index < 0)
449*4882a593Smuzhiyun 		return -EINVAL;
450*4882a593Smuzhiyun 	with_address = spi_setup_offset(trans);
451*4882a593Smuzhiyun 	if (with_address < 0)
452*4882a593Smuzhiyun 		return -EINVAL;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if (trans->opcode == SPI_OPCODE_WREN) {
455*4882a593Smuzhiyun 		/*
456*4882a593Smuzhiyun 		 * Treat Write Enable as Atomic Pre-Op if possible
457*4882a593Smuzhiyun 		 * in order to prevent the Management Engine from
458*4882a593Smuzhiyun 		 * issuing a transaction between WREN and DATA.
459*4882a593Smuzhiyun 		 */
460*4882a593Smuzhiyun 		if (!lock)
461*4882a593Smuzhiyun 			ich_writew(ctlr, trans->opcode, ctlr->preop);
462*4882a593Smuzhiyun 		return 0;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (ctlr->speed && ctlr->max_speed >= 33000000) {
466*4882a593Smuzhiyun 		int byte;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		byte = ich_readb(ctlr, ctlr->speed);
469*4882a593Smuzhiyun 		if (ctlr->cur_speed >= 33000000)
470*4882a593Smuzhiyun 			byte |= SSFC_SCF_33MHZ;
471*4882a593Smuzhiyun 		else
472*4882a593Smuzhiyun 			byte &= ~SSFC_SCF_33MHZ;
473*4882a593Smuzhiyun 		ich_writeb(ctlr, byte, ctlr->speed);
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	/* See if we have used up the command data */
477*4882a593Smuzhiyun 	if (using_cmd && dout && bytes) {
478*4882a593Smuzhiyun 		trans->out = dout;
479*4882a593Smuzhiyun 		trans->bytesout = bytes;
480*4882a593Smuzhiyun 		debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* Preset control fields */
484*4882a593Smuzhiyun 	control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/* Issue atomic preop cycle if needed */
487*4882a593Smuzhiyun 	if (ich_readw(ctlr, ctlr->preop))
488*4882a593Smuzhiyun 		control |= SPIC_ACS;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	if (!trans->bytesout && !trans->bytesin) {
491*4882a593Smuzhiyun 		/* SPI addresses are 24 bit only */
492*4882a593Smuzhiyun 		if (with_address) {
493*4882a593Smuzhiyun 			ich_writel(ctlr, trans->offset & 0x00FFFFFF,
494*4882a593Smuzhiyun 				   ctlr->addr);
495*4882a593Smuzhiyun 		}
496*4882a593Smuzhiyun 		/*
497*4882a593Smuzhiyun 		 * This is a 'no data' command (like Write Enable), its
498*4882a593Smuzhiyun 		 * bitesout size was 1, decremented to zero while executing
499*4882a593Smuzhiyun 		 * spi_setup_opcode() above. Tell the chip to send the
500*4882a593Smuzhiyun 		 * command.
501*4882a593Smuzhiyun 		 */
502*4882a593Smuzhiyun 		ich_writew(ctlr, control, ctlr->control);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		/* wait for the result */
505*4882a593Smuzhiyun 		status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
506*4882a593Smuzhiyun 		if (status < 0)
507*4882a593Smuzhiyun 			return status;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		if (status & SPIS_FCERR) {
510*4882a593Smuzhiyun 			debug("ICH SPI: Command transaction error\n");
511*4882a593Smuzhiyun 			return -EIO;
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		return 0;
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	/*
518*4882a593Smuzhiyun 	 * Check if this is a write command atempting to transfer more bytes
519*4882a593Smuzhiyun 	 * than the controller can handle. Iterations for writes are not
520*4882a593Smuzhiyun 	 * supported here because each SPI write command needs to be preceded
521*4882a593Smuzhiyun 	 * and followed by other SPI commands, and this sequence is controlled
522*4882a593Smuzhiyun 	 * by the SPI chip driver.
523*4882a593Smuzhiyun 	 */
524*4882a593Smuzhiyun 	if (trans->bytesout > ctlr->databytes) {
525*4882a593Smuzhiyun 		debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
526*4882a593Smuzhiyun 		return -EPROTO;
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	/*
530*4882a593Smuzhiyun 	 * Read or write up to databytes bytes at a time until everything has
531*4882a593Smuzhiyun 	 * been sent.
532*4882a593Smuzhiyun 	 */
533*4882a593Smuzhiyun 	while (trans->bytesout || trans->bytesin) {
534*4882a593Smuzhiyun 		uint32_t data_length;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 		/* SPI addresses are 24 bit only */
537*4882a593Smuzhiyun 		ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		if (trans->bytesout)
540*4882a593Smuzhiyun 			data_length = min(trans->bytesout, ctlr->databytes);
541*4882a593Smuzhiyun 		else
542*4882a593Smuzhiyun 			data_length = min(trans->bytesin, ctlr->databytes);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 		/* Program data into FDATA0 to N */
545*4882a593Smuzhiyun 		if (trans->bytesout) {
546*4882a593Smuzhiyun 			write_reg(ctlr, trans->out, ctlr->data, data_length);
547*4882a593Smuzhiyun 			spi_use_out(trans, data_length);
548*4882a593Smuzhiyun 			if (with_address)
549*4882a593Smuzhiyun 				trans->offset += data_length;
550*4882a593Smuzhiyun 		}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 		/* Add proper control fields' values */
553*4882a593Smuzhiyun 		control &= ~((ctlr->databytes - 1) << 8);
554*4882a593Smuzhiyun 		control |= SPIC_DS;
555*4882a593Smuzhiyun 		control |= (data_length - 1) << 8;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		/* write it */
558*4882a593Smuzhiyun 		ich_writew(ctlr, control, ctlr->control);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		/* Wait for Cycle Done Status or Flash Cycle Error */
561*4882a593Smuzhiyun 		status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
562*4882a593Smuzhiyun 		if (status < 0)
563*4882a593Smuzhiyun 			return status;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		if (status & SPIS_FCERR) {
566*4882a593Smuzhiyun 			debug("ICH SPI: Data transaction error %x\n", status);
567*4882a593Smuzhiyun 			return -EIO;
568*4882a593Smuzhiyun 		}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		if (trans->bytesin) {
571*4882a593Smuzhiyun 			read_reg(ctlr, ctlr->data, trans->in, data_length);
572*4882a593Smuzhiyun 			spi_use_in(trans, data_length);
573*4882a593Smuzhiyun 			if (with_address)
574*4882a593Smuzhiyun 				trans->offset += data_length;
575*4882a593Smuzhiyun 		}
576*4882a593Smuzhiyun 	}
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	/* Clear atomic preop now that xfer is done */
579*4882a593Smuzhiyun 	if (!lock)
580*4882a593Smuzhiyun 		ich_writew(ctlr, 0, ctlr->preop);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	return 0;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
ich_spi_probe(struct udevice * dev)585*4882a593Smuzhiyun static int ich_spi_probe(struct udevice *dev)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	struct ich_spi_platdata *plat = dev_get_platdata(dev);
588*4882a593Smuzhiyun 	struct ich_spi_priv *priv = dev_get_priv(dev);
589*4882a593Smuzhiyun 	uint8_t bios_cntl;
590*4882a593Smuzhiyun 	int ret;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	ret = ich_init_controller(dev, plat, priv);
593*4882a593Smuzhiyun 	if (ret)
594*4882a593Smuzhiyun 		return ret;
595*4882a593Smuzhiyun 	/* Disable the BIOS write protect so write commands are allowed */
596*4882a593Smuzhiyun 	ret = pch_set_spi_protect(dev->parent, false);
597*4882a593Smuzhiyun 	if (ret == -ENOSYS) {
598*4882a593Smuzhiyun 		bios_cntl = ich_readb(priv, priv->bcr);
599*4882a593Smuzhiyun 		bios_cntl &= ~BIT(5);	/* clear Enable InSMM_STS (EISS) */
600*4882a593Smuzhiyun 		bios_cntl |= 1;		/* Write Protect Disable (WPD) */
601*4882a593Smuzhiyun 		ich_writeb(priv, bios_cntl, priv->bcr);
602*4882a593Smuzhiyun 	} else if (ret) {
603*4882a593Smuzhiyun 		debug("%s: Failed to disable write-protect: err=%d\n",
604*4882a593Smuzhiyun 		      __func__, ret);
605*4882a593Smuzhiyun 		return ret;
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	/* Lock down SPI controller settings if required */
609*4882a593Smuzhiyun 	if (plat->lockdown) {
610*4882a593Smuzhiyun 		ich_spi_config_opcode(dev);
611*4882a593Smuzhiyun 		spi_lock_down(plat, priv->base);
612*4882a593Smuzhiyun 	}
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	priv->cur_speed = priv->max_speed;
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	return 0;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
ich_spi_remove(struct udevice * bus)619*4882a593Smuzhiyun static int ich_spi_remove(struct udevice *bus)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	/*
622*4882a593Smuzhiyun 	 * Configure SPI controller so that the Linux MTD driver can fully
623*4882a593Smuzhiyun 	 * access the SPI NOR chip
624*4882a593Smuzhiyun 	 */
625*4882a593Smuzhiyun 	ich_spi_config_opcode(bus);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	return 0;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
ich_spi_set_speed(struct udevice * bus,uint speed)630*4882a593Smuzhiyun static int ich_spi_set_speed(struct udevice *bus, uint speed)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	struct ich_spi_priv *priv = dev_get_priv(bus);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	priv->cur_speed = speed;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	return 0;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
ich_spi_set_mode(struct udevice * bus,uint mode)639*4882a593Smuzhiyun static int ich_spi_set_mode(struct udevice *bus, uint mode)
640*4882a593Smuzhiyun {
641*4882a593Smuzhiyun 	debug("%s: mode=%d\n", __func__, mode);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	return 0;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
ich_spi_child_pre_probe(struct udevice * dev)646*4882a593Smuzhiyun static int ich_spi_child_pre_probe(struct udevice *dev)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun 	struct udevice *bus = dev_get_parent(dev);
649*4882a593Smuzhiyun 	struct ich_spi_platdata *plat = dev_get_platdata(bus);
650*4882a593Smuzhiyun 	struct ich_spi_priv *priv = dev_get_priv(bus);
651*4882a593Smuzhiyun 	struct spi_slave *slave = dev_get_parent_priv(dev);
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/*
654*4882a593Smuzhiyun 	 * Yes this controller can only write a small number of bytes at
655*4882a593Smuzhiyun 	 * once! The limit is typically 64 bytes.
656*4882a593Smuzhiyun 	 */
657*4882a593Smuzhiyun 	slave->max_write_size = priv->databytes;
658*4882a593Smuzhiyun 	/*
659*4882a593Smuzhiyun 	 * ICH 7 SPI controller only supports array read command
660*4882a593Smuzhiyun 	 * and byte program command for SST flash
661*4882a593Smuzhiyun 	 */
662*4882a593Smuzhiyun 	if (plat->ich_version == ICHV_7)
663*4882a593Smuzhiyun 		slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	return 0;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
ich_spi_ofdata_to_platdata(struct udevice * dev)668*4882a593Smuzhiyun static int ich_spi_ofdata_to_platdata(struct udevice *dev)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct ich_spi_platdata *plat = dev_get_platdata(dev);
671*4882a593Smuzhiyun 	int node = dev_of_offset(dev);
672*4882a593Smuzhiyun 	int ret;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
675*4882a593Smuzhiyun 	if (ret == 0) {
676*4882a593Smuzhiyun 		plat->ich_version = ICHV_7;
677*4882a593Smuzhiyun 	} else {
678*4882a593Smuzhiyun 		ret = fdt_node_check_compatible(gd->fdt_blob, node,
679*4882a593Smuzhiyun 						"intel,ich9-spi");
680*4882a593Smuzhiyun 		if (ret == 0)
681*4882a593Smuzhiyun 			plat->ich_version = ICHV_9;
682*4882a593Smuzhiyun 	}
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
685*4882a593Smuzhiyun 					 "intel,spi-lock-down");
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	return ret;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun static const struct dm_spi_ops ich_spi_ops = {
691*4882a593Smuzhiyun 	.xfer		= ich_spi_xfer,
692*4882a593Smuzhiyun 	.set_speed	= ich_spi_set_speed,
693*4882a593Smuzhiyun 	.set_mode	= ich_spi_set_mode,
694*4882a593Smuzhiyun 	/*
695*4882a593Smuzhiyun 	 * cs_info is not needed, since we require all chip selects to be
696*4882a593Smuzhiyun 	 * in the device tree explicitly
697*4882a593Smuzhiyun 	 */
698*4882a593Smuzhiyun };
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun static const struct udevice_id ich_spi_ids[] = {
701*4882a593Smuzhiyun 	{ .compatible = "intel,ich7-spi" },
702*4882a593Smuzhiyun 	{ .compatible = "intel,ich9-spi" },
703*4882a593Smuzhiyun 	{ }
704*4882a593Smuzhiyun };
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun U_BOOT_DRIVER(ich_spi) = {
707*4882a593Smuzhiyun 	.name	= "ich_spi",
708*4882a593Smuzhiyun 	.id	= UCLASS_SPI,
709*4882a593Smuzhiyun 	.of_match = ich_spi_ids,
710*4882a593Smuzhiyun 	.ops	= &ich_spi_ops,
711*4882a593Smuzhiyun 	.ofdata_to_platdata = ich_spi_ofdata_to_platdata,
712*4882a593Smuzhiyun 	.platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
713*4882a593Smuzhiyun 	.priv_auto_alloc_size = sizeof(struct ich_spi_priv),
714*4882a593Smuzhiyun 	.child_pre_probe = ich_spi_child_pre_probe,
715*4882a593Smuzhiyun 	.probe	= ich_spi_probe,
716*4882a593Smuzhiyun 	.remove	= ich_spi_remove,
717*4882a593Smuzhiyun 	.flags	= DM_FLAG_OS_PREPARE,
718*4882a593Smuzhiyun };
719