xref: /openbmc/u-boot/drivers/spi/exynos_spi.c (revision 68fbc0e6)
1 /*
2  * (C) Copyright 2012 SAMSUNG Electronics
3  * Padmavathi Venna <padma.v@samsung.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #include <common.h>
21 #include <malloc.h>
22 #include <spi.h>
23 #include <fdtdec.h>
24 #include <asm/arch/clk.h>
25 #include <asm/arch/clock.h>
26 #include <asm/arch/cpu.h>
27 #include <asm/arch/gpio.h>
28 #include <asm/arch/pinmux.h>
29 #include <asm/arch-exynos/spi.h>
30 #include <asm/io.h>
31 
32 DECLARE_GLOBAL_DATA_PTR;
33 
34 /* Information about each SPI controller */
35 struct spi_bus {
36 	enum periph_id periph_id;
37 	s32 frequency;		/* Default clock frequency, -1 for none */
38 	struct exynos_spi *regs;
39 	int inited;		/* 1 if this bus is ready for use */
40 	int node;
41 };
42 
43 /* A list of spi buses that we know about */
44 static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
45 static unsigned int bus_count;
46 
47 struct exynos_spi_slave {
48 	struct spi_slave slave;
49 	struct exynos_spi *regs;
50 	unsigned int freq;		/* Default frequency */
51 	unsigned int mode;
52 	enum periph_id periph_id;	/* Peripheral ID for this device */
53 	unsigned int fifo_size;
54 };
55 
56 static struct spi_bus *spi_get_bus(unsigned dev_index)
57 {
58 	if (dev_index < bus_count)
59 		return &spi_bus[dev_index];
60 	debug("%s: invalid bus %d", __func__, dev_index);
61 
62 	return NULL;
63 }
64 
65 static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
66 {
67 	return container_of(slave, struct exynos_spi_slave, slave);
68 }
69 
70 /**
71  * Setup the driver private data
72  *
73  * @param bus		ID of the bus that the slave is attached to
74  * @param cs		ID of the chip select connected to the slave
75  * @param max_hz	Required spi frequency
76  * @param mode		Required spi mode (clk polarity, clk phase and
77  *			master or slave)
78  * @return new device or NULL
79  */
80 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
81 			unsigned int max_hz, unsigned int mode)
82 {
83 	struct exynos_spi_slave *spi_slave;
84 	struct spi_bus *bus;
85 
86 	if (!spi_cs_is_valid(busnum, cs)) {
87 		debug("%s: Invalid bus/chip select %d, %d\n", __func__,
88 		      busnum, cs);
89 		return NULL;
90 	}
91 
92 	spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
93 	if (!spi_slave) {
94 		debug("%s: Could not allocate spi_slave\n", __func__);
95 		return NULL;
96 	}
97 
98 	bus = &spi_bus[busnum];
99 	spi_slave->regs = bus->regs;
100 	spi_slave->mode = mode;
101 	spi_slave->periph_id = bus->periph_id;
102 	if (bus->periph_id == PERIPH_ID_SPI1 ||
103 	    bus->periph_id == PERIPH_ID_SPI2)
104 		spi_slave->fifo_size = 64;
105 	else
106 		spi_slave->fifo_size = 256;
107 
108 	spi_slave->freq = bus->frequency;
109 	if (max_hz)
110 		spi_slave->freq = min(max_hz, spi_slave->freq);
111 
112 	return &spi_slave->slave;
113 }
114 
115 /**
116  * Free spi controller
117  *
118  * @param slave	Pointer to spi_slave to which controller has to
119  *		communicate with
120  */
121 void spi_free_slave(struct spi_slave *slave)
122 {
123 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
124 
125 	free(spi_slave);
126 }
127 
128 /**
129  * Flush spi tx, rx fifos and reset the SPI controller
130  *
131  * @param slave	Pointer to spi_slave to which controller has to
132  *		communicate with
133  */
134 static void spi_flush_fifo(struct spi_slave *slave)
135 {
136 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
137 	struct exynos_spi *regs = spi_slave->regs;
138 
139 	clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
140 	clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
141 	setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
142 }
143 
144 /**
145  * Initialize the spi base registers, set the required clock frequency and
146  * initialize the gpios
147  *
148  * @param slave	Pointer to spi_slave to which controller has to
149  *		communicate with
150  * @return zero on success else a negative value
151  */
152 int spi_claim_bus(struct spi_slave *slave)
153 {
154 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
155 	struct exynos_spi *regs = spi_slave->regs;
156 	u32 reg = 0;
157 	int ret;
158 
159 	ret = set_spi_clk(spi_slave->periph_id,
160 					spi_slave->freq);
161 	if (ret < 0) {
162 		debug("%s: Failed to setup spi clock\n", __func__);
163 		return ret;
164 	}
165 
166 	exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
167 
168 	spi_flush_fifo(slave);
169 
170 	reg = readl(&regs->ch_cfg);
171 	reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
172 
173 	if (spi_slave->mode & SPI_CPHA)
174 		reg |= SPI_CH_CPHA_B;
175 
176 	if (spi_slave->mode & SPI_CPOL)
177 		reg |= SPI_CH_CPOL_L;
178 
179 	writel(reg, &regs->ch_cfg);
180 	writel(SPI_FB_DELAY_180, &regs->fb_clk);
181 
182 	return 0;
183 }
184 
185 /**
186  * Reset the spi H/W and flush the tx and rx fifos
187  *
188  * @param slave	Pointer to spi_slave to which controller has to
189  *		communicate with
190  */
191 void spi_release_bus(struct spi_slave *slave)
192 {
193 	spi_flush_fifo(slave);
194 }
195 
196 static void spi_get_fifo_levels(struct exynos_spi *regs,
197 	int *rx_lvl, int *tx_lvl)
198 {
199 	uint32_t spi_sts = readl(&regs->spi_sts);
200 
201 	*rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
202 	*tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
203 }
204 
205 /**
206  * If there's something to transfer, do a software reset and set a
207  * transaction size.
208  *
209  * @param regs	SPI peripheral registers
210  * @param count	Number of bytes to transfer
211  */
212 static void spi_request_bytes(struct exynos_spi *regs, int count)
213 {
214 	assert(count && count < (1 << 16));
215 	setbits_le32(&regs->ch_cfg, SPI_CH_RST);
216 	clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
217 	writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
218 }
219 
220 static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
221 			void **dinp, void const **doutp)
222 {
223 	struct exynos_spi *regs = spi_slave->regs;
224 	uchar *rxp = *dinp;
225 	const uchar *txp = *doutp;
226 	int rx_lvl, tx_lvl;
227 	uint out_bytes, in_bytes;
228 
229 	out_bytes = in_bytes = todo;
230 
231 	/*
232 	 * If there's something to send, do a software reset and set a
233 	 * transaction size.
234 	 */
235 	spi_request_bytes(regs, todo);
236 
237 	/*
238 	 * Bytes are transmitted/received in pairs. Wait to receive all the
239 	 * data because then transmission will be done as well.
240 	 */
241 	while (in_bytes) {
242 		int temp;
243 
244 		/* Keep the fifos full/empty. */
245 		spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
246 		if (tx_lvl < spi_slave->fifo_size && out_bytes) {
247 			temp = txp ? *txp++ : 0xff;
248 			writel(temp, &regs->tx_data);
249 			out_bytes--;
250 		}
251 		if (rx_lvl > 0 && in_bytes) {
252 			temp = readl(&regs->rx_data);
253 			if (rxp)
254 				*rxp++ = temp;
255 			in_bytes--;
256 		}
257 	}
258 	*dinp = rxp;
259 	*doutp = txp;
260 }
261 
262 /**
263  * Transfer and receive data
264  *
265  * @param slave		Pointer to spi_slave to which controller has to
266  *			communicate with
267  * @param bitlen	No of bits to tranfer or receive
268  * @param dout		Pointer to transfer buffer
269  * @param din		Pointer to receive buffer
270  * @param flags		Flags for transfer begin and end
271  * @return zero on success else a negative value
272  */
273 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
274 	     void *din, unsigned long flags)
275 {
276 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
277 	int upto, todo;
278 	int bytelen;
279 
280 	/* spi core configured to do 8 bit transfers */
281 	if (bitlen % 8) {
282 		debug("Non byte aligned SPI transfer.\n");
283 		return -1;
284 	}
285 
286 	/* Start the transaction, if necessary. */
287 	if ((flags & SPI_XFER_BEGIN))
288 		spi_cs_activate(slave);
289 
290 	/* Exynos SPI limits each transfer to 65535 bytes */
291 	bytelen =  bitlen / 8;
292 	for (upto = 0; upto < bytelen; upto += todo) {
293 		todo = min(bytelen - upto, (1 << 16) - 1);
294 		spi_rx_tx(spi_slave, todo, &din, &dout);
295 	}
296 
297 	/* Stop the transaction, if necessary. */
298 	if ((flags & SPI_XFER_END))
299 		spi_cs_deactivate(slave);
300 
301 	return 0;
302 }
303 
304 /**
305  * Validates the bus and chip select numbers
306  *
307  * @param bus	ID of the bus that the slave is attached to
308  * @param cs	ID of the chip select connected to the slave
309  * @return one on success else zero
310  */
311 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
312 {
313 	return spi_get_bus(bus) && cs == 0;
314 }
315 
316 /**
317  * Activate the CS by driving it LOW
318  *
319  * @param slave	Pointer to spi_slave to which controller has to
320  *		communicate with
321  */
322 void spi_cs_activate(struct spi_slave *slave)
323 {
324 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
325 
326 	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
327 	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
328 }
329 
330 /**
331  * Deactivate the CS by driving it HIGH
332  *
333  * @param slave	Pointer to spi_slave to which controller has to
334  *		communicate with
335  */
336 void spi_cs_deactivate(struct spi_slave *slave)
337 {
338 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
339 
340 	setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
341 	debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
342 }
343 
344 static inline struct exynos_spi *get_spi_base(int dev_index)
345 {
346 	if (dev_index < 3)
347 		return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
348 	else
349 		return (struct exynos_spi *)samsung_get_base_spi_isp() +
350 					(dev_index - 3);
351 }
352 
353 /*
354  * Read the SPI config from the device tree node.
355  *
356  * @param blob  FDT blob to read from
357  * @param node  Node offset to read from
358  * @param bus   SPI bus structure to fill with information
359  * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
360  */
361 #ifdef CONFIG_OF_CONTROL
362 static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
363 {
364 	bus->node = node;
365 	bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
366 	bus->periph_id = pinmux_decode_periph_id(blob, node);
367 
368 	if (bus->periph_id == PERIPH_ID_NONE) {
369 		debug("%s: Invalid peripheral ID %d\n", __func__,
370 			bus->periph_id);
371 		return -FDT_ERR_NOTFOUND;
372 	}
373 
374 	/* Use 500KHz as a suitable default */
375 	bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
376 					500000);
377 
378 	return 0;
379 }
380 
381 /*
382  * Process a list of nodes, adding them to our list of SPI ports.
383  *
384  * @param blob          fdt blob
385  * @param node_list     list of nodes to process (any <=0 are ignored)
386  * @param count         number of nodes to process
387  * @param is_dvc        1 if these are DVC ports, 0 if standard I2C
388  * @return 0 if ok, -1 on error
389  */
390 static int process_nodes(const void *blob, int node_list[], int count)
391 {
392 	int i;
393 
394 	/* build the i2c_controllers[] for each controller */
395 	for (i = 0; i < count; i++) {
396 		int node = node_list[i];
397 		struct spi_bus *bus;
398 
399 		if (node <= 0)
400 			continue;
401 
402 		bus = &spi_bus[i];
403 		if (spi_get_config(blob, node, bus)) {
404 			printf("exynos spi_init: failed to decode bus %d\n",
405 				i);
406 			return -1;
407 		}
408 
409 		debug("spi: controller bus %d at %p, periph_id %d\n",
410 		      i, bus->regs, bus->periph_id);
411 		bus->inited = 1;
412 		bus_count++;
413 	}
414 
415 	return 0;
416 }
417 #endif
418 
419 /* Sadly there is no error return from this function */
420 void spi_init(void)
421 {
422 	int count;
423 
424 #ifdef CONFIG_OF_CONTROL
425 	int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
426 	const void *blob = gd->fdt_blob;
427 
428 	count = fdtdec_find_aliases_for_id(blob, "spi",
429 			COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
430 			EXYNOS5_SPI_NUM_CONTROLLERS);
431 	if (process_nodes(blob, node_list, count))
432 		return;
433 
434 #else
435 	struct spi_bus *bus;
436 
437 	for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
438 		bus = &spi_bus[count];
439 		bus->regs = get_spi_base(count);
440 		bus->periph_id = PERIPH_ID_SPI0 + count;
441 
442 		/* Although Exynos5 supports upto 50Mhz speed,
443 		 * we are setting it to 10Mhz for safe side
444 		 */
445 		bus->frequency = 10000000;
446 		bus->inited = 1;
447 		bus->node = 0;
448 		bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
449 	}
450 #endif
451 }
452