xref: /freebsd/sys/arm/ti/ti_i2c.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*-
2  * Copyright (c) 2011 Ben Gray <ben.r.gray@gmail.com>.
3  * Copyright (c) 2014 Luiz Otavio O Souza <loos@freebsd.org>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /**
29  * Driver for the I2C module on the TI SoC.
30  *
31  * This driver is heavily based on the TWI driver for the AT91 (at91_twi.c).
32  *
33  * CAUTION: The I2Ci registers are limited to 16 bit and 8 bit data accesses,
34  * 32 bit data access is not allowed and can corrupt register content.
35  *
36  * This driver currently doesn't use DMA for the transfer, although I hope to
37  * incorporate that sometime in the future.  The idea being that for transaction
38  * larger than a certain size the DMA engine is used, for anything less the
39  * normal interrupt/fifo driven option is used.
40  *
41  *
42  * WARNING: This driver uses mtx_sleep and interrupts to perform transactions,
43  * which means you can't do a transaction during startup before the interrupts
44  * have been enabled.  Hint - the freebsd function config_intrhook_establish().
45  */
46 
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/bus.h>
53 #include <sys/conf.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/mbuf.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/rman.h>
61 #include <sys/sysctl.h>
62 #include <machine/bus.h>
63 
64 #include <dev/ofw/openfirm.h>
65 #include <dev/ofw/ofw_bus.h>
66 #include <dev/ofw/ofw_bus_subr.h>
67 
68 #include <arm/ti/ti_cpuid.h>
69 #include <arm/ti/ti_prcm.h>
70 #include <arm/ti/ti_i2c.h>
71 
72 #include <dev/iicbus/iiconf.h>
73 #include <dev/iicbus/iicbus.h>
74 
75 #include "iicbus_if.h"
76 
77 /**
78  *	I2C device driver context, a pointer to this is stored in the device
79  *	driver structure.
80  */
81 struct ti_i2c_softc
82 {
83 	device_t		sc_dev;
84 	uint32_t		device_id;
85 	struct resource*	sc_irq_res;
86 	struct resource*	sc_mem_res;
87 	device_t		sc_iicbus;
88 
89 	void*			sc_irq_h;
90 
91 	struct mtx		sc_mtx;
92 
93 	struct iic_msg*		sc_buffer;
94 	int			sc_bus_inuse;
95 	int			sc_buffer_pos;
96 	int			sc_error;
97 	int			sc_fifo_trsh;
98 
99 	uint16_t		sc_con_reg;
100 	uint16_t		sc_rev;
101 };
102 
103 struct ti_i2c_clock_config
104 {
105 	int speed;
106 	int bitrate;
107 	uint8_t psc;		/* Fast/Standard mode prescale divider */
108 	uint8_t scll;		/* Fast/Standard mode SCL low time */
109 	uint8_t sclh;		/* Fast/Standard mode SCL high time */
110 	uint8_t hsscll;		/* High Speed mode SCL low time */
111 	uint8_t hssclh;		/* High Speed mode SCL high time */
112 };
113 
114 #if defined(SOC_OMAP3)
115 #error "Unsupported SoC"
116 #endif
117 
118 #if defined(SOC_OMAP4)
119 static struct ti_i2c_clock_config ti_omap4_i2c_clock_configs[] = {
120 	{ IIC_UNKNOWN,	 100000, 23, 13, 15,  0, 0},
121 	{ IIC_SLOW,	 100000, 23, 13, 15,  0, 0},
122 	{ IIC_FAST,	 400000,  9,  5,  7,  0, 0},
123 	{ IIC_FASTEST,	1000000,  5,  3,  4,  0, 0},
124 	/* { IIC_FASTEST, 3200000,  1, 113, 115, 7, 10}, - HS mode */
125 	{ -1, 0 }
126 };
127 #endif
128 
129 #if defined(SOC_TI_AM335X)
130 /*
131  * AM335X doesn't support HS mode.  For 100kHz I2C clock set the internal
132  * clock to 12Mhz, for 400kHz I2C clock set the internal clock to 24Mhz.
133  */
134 static struct ti_i2c_clock_config ti_am335x_i2c_clock_configs[] = {
135 	{ IIC_UNKNOWN,	 100000, 7, 59, 61, 0, 0},
136 	{ IIC_SLOW,	 100000, 7, 59, 61, 0, 0},
137 	{ IIC_FAST,	 400000, 3, 23, 25, 0, 0},
138 	{ IIC_FASTEST,	 400000, 3, 23, 25, 0, 0},
139 	{ -1, 0 }
140 };
141 #endif
142 
143 /**
144  *	Locking macros used throughout the driver
145  */
146 #define	TI_I2C_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
147 #define	TI_I2C_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
148 #define	TI_I2C_LOCK_INIT(_sc)						\
149 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev),	\
150 	    "ti_i2c", MTX_DEF)
151 #define	TI_I2C_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx)
152 #define	TI_I2C_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED)
153 #define	TI_I2C_ASSERT_UNLOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
154 
155 #ifdef DEBUG
156 #define	ti_i2c_dbg(_sc, fmt, args...)					\
157 	device_printf((_sc)->sc_dev, fmt, ##args)
158 #else
159 #define	ti_i2c_dbg(_sc, fmt, args...)
160 #endif
161 
162 /**
163  *	ti_i2c_read_2 - reads a 16-bit value from one of the I2C registers
164  *	@sc: I2C device context
165  *	@off: the byte offset within the register bank to read from.
166  *
167  *
168  *	LOCKING:
169  *	No locking required
170  *
171  *	RETURNS:
172  *	16-bit value read from the register.
173  */
174 static inline uint16_t
175 ti_i2c_read_2(struct ti_i2c_softc *sc, bus_size_t off)
176 {
177 
178 	return (bus_read_2(sc->sc_mem_res, off));
179 }
180 
181 /**
182  *	ti_i2c_write_2 - writes a 16-bit value to one of the I2C registers
183  *	@sc: I2C device context
184  *	@off: the byte offset within the register bank to read from.
185  *	@val: the value to write into the register
186  *
187  *	LOCKING:
188  *	No locking required
189  *
190  *	RETURNS:
191  *	16-bit value read from the register.
192  */
193 static inline void
194 ti_i2c_write_2(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)
195 {
196 
197 	bus_write_2(sc->sc_mem_res, off, val);
198 }
199 
200 static int
201 ti_i2c_transfer_intr(struct ti_i2c_softc* sc, uint16_t status)
202 {
203 	int amount, done, i;
204 
205 	done = 0;
206 	amount = 0;
207 	/* Check for the error conditions. */
208 	if (status & I2C_STAT_NACK) {
209 		/* No ACK from slave. */
210 		ti_i2c_dbg(sc, "NACK\n");
211 		ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_NACK);
212 		sc->sc_error = ENXIO;
213 	} else if (status & I2C_STAT_AL) {
214 		/* Arbitration lost. */
215 		ti_i2c_dbg(sc, "Arbitration lost\n");
216 		ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_AL);
217 		sc->sc_error = ENXIO;
218 	}
219 
220 	/* Check if we have finished. */
221 	if (status & I2C_STAT_ARDY) {
222 		/* Register access ready - transaction complete basically. */
223 		ti_i2c_dbg(sc, "ARDY transaction complete\n");
224 		if (sc->sc_error != 0 && sc->sc_buffer->flags & IIC_M_NOSTOP) {
225 			ti_i2c_write_2(sc, I2C_REG_CON,
226 			    sc->sc_con_reg | I2C_CON_STP);
227 		}
228 		ti_i2c_write_2(sc, I2C_REG_STATUS,
229 		    I2C_STAT_ARDY | I2C_STAT_RDR | I2C_STAT_RRDY |
230 		    I2C_STAT_XDR | I2C_STAT_XRDY);
231 		return (1);
232 	}
233 
234 	if (sc->sc_buffer->flags & IIC_M_RD) {
235 		/* Read some data. */
236 		if (status & I2C_STAT_RDR) {
237 			/*
238 			 * Receive draining interrupt - last data received.
239 			 * The set FIFO threshold wont be reached to trigger
240 			 * RRDY.
241 			 */
242 			ti_i2c_dbg(sc, "Receive draining interrupt\n");
243 
244 			/*
245 			 * Drain the FIFO.  Read the pending data in the FIFO.
246 			 */
247 			amount = sc->sc_buffer->len - sc->sc_buffer_pos;
248 		} else if (status & I2C_STAT_RRDY) {
249 			/*
250 			 * Receive data ready interrupt - FIFO has reached the
251 			 * set threshold.
252 			 */
253 			ti_i2c_dbg(sc, "Receive data ready interrupt\n");
254 
255 			amount = min(sc->sc_fifo_trsh,
256 			    sc->sc_buffer->len - sc->sc_buffer_pos);
257 		}
258 
259 		/* Read the bytes from the fifo. */
260 		for (i = 0; i < amount; i++)
261 			sc->sc_buffer->buf[sc->sc_buffer_pos++] =
262 			    (uint8_t)(ti_i2c_read_2(sc, I2C_REG_DATA) & 0xff);
263 
264 		if (status & I2C_STAT_RDR)
265 			ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RDR);
266 		if (status & I2C_STAT_RRDY)
267 			ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RRDY);
268 
269 	} else {
270 		/* Write some data. */
271 		if (status & I2C_STAT_XDR) {
272 			/*
273 			 * Transmit draining interrupt - FIFO level is below
274 			 * the set threshold and the amount of data still to
275 			 * be transferred wont reach the set FIFO threshold.
276 			 */
277 			ti_i2c_dbg(sc, "Transmit draining interrupt\n");
278 
279 			/*
280 			 * Drain the TX data.  Write the pending data in the
281 			 * FIFO.
282 			 */
283 			amount = sc->sc_buffer->len - sc->sc_buffer_pos;
284 		} else if (status & I2C_STAT_XRDY) {
285 			/*
286 			 * Transmit data ready interrupt - the FIFO level
287 			 * is below the set threshold.
288 			 */
289 			ti_i2c_dbg(sc, "Transmit data ready interrupt\n");
290 
291 			amount = min(sc->sc_fifo_trsh,
292 			    sc->sc_buffer->len - sc->sc_buffer_pos);
293 		}
294 
295 		/* Write the bytes from the fifo. */
296 		for (i = 0; i < amount; i++)
297 			ti_i2c_write_2(sc, I2C_REG_DATA,
298 			    sc->sc_buffer->buf[sc->sc_buffer_pos++]);
299 
300 		if (status & I2C_STAT_XDR)
301 			ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XDR);
302 		if (status & I2C_STAT_XRDY)
303 			ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XRDY);
304 	}
305 
306 	return (done);
307 }
308 
309 /**
310  *	ti_i2c_intr - interrupt handler for the I2C module
311  *	@dev: i2c device handle
312  *
313  *
314  *
315  *	LOCKING:
316  *	Called from timer context
317  *
318  *	RETURNS:
319  *	EH_HANDLED or EH_NOT_HANDLED
320  */
321 static void
322 ti_i2c_intr(void *arg)
323 {
324 	int done;
325 	struct ti_i2c_softc *sc;
326 	uint16_t events, status;
327 
328  	sc = (struct ti_i2c_softc *)arg;
329 
330 	TI_I2C_LOCK(sc);
331 
332 	status = ti_i2c_read_2(sc, I2C_REG_STATUS);
333 	if (status == 0) {
334 		TI_I2C_UNLOCK(sc);
335 		return;
336 	}
337 
338 	/* Save enabled interrupts. */
339 	events = ti_i2c_read_2(sc, I2C_REG_IRQENABLE_SET);
340 
341 	/* We only care about enabled interrupts. */
342 	status &= events;
343 
344 	done = 0;
345 
346 	if (sc->sc_buffer != NULL)
347 		done = ti_i2c_transfer_intr(sc, status);
348 	else {
349 		ti_i2c_dbg(sc, "Transfer interrupt without buffer\n");
350 		sc->sc_error = EINVAL;
351 		done = 1;
352 	}
353 
354 	if (done)
355 		/* Wakeup the process that started the transaction. */
356 		wakeup(sc);
357 
358 	TI_I2C_UNLOCK(sc);
359 }
360 
361 /**
362  *	ti_i2c_transfer - called to perform the transfer
363  *	@dev: i2c device handle
364  *	@msgs: the messages to send/receive
365  *	@nmsgs: the number of messages in the msgs array
366  *
367  *
368  *	LOCKING:
369  *	Internally locked
370  *
371  *	RETURNS:
372  *	0 on function succeeded
373  *	EINVAL if invalid message is passed as an arg
374  */
375 static int
376 ti_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
377 {
378 	int err, i, repstart, timeout;
379 	struct ti_i2c_softc *sc;
380 	uint16_t reg;
381 
382  	sc = device_get_softc(dev);
383 	TI_I2C_LOCK(sc);
384 
385 	/* If the controller is busy wait until it is available. */
386 	while (sc->sc_bus_inuse == 1)
387 		mtx_sleep(dev, &sc->sc_mtx, 0, "i2cbuswait", 0);
388 
389 	/* Now we have control over the I2C controller. */
390 	sc->sc_bus_inuse = 1;
391 
392 	err = 0;
393 	repstart = 0;
394 	for (i = 0; i < nmsgs; i++) {
395 
396 		sc->sc_buffer = &msgs[i];
397 		sc->sc_buffer_pos = 0;
398 		sc->sc_error = 0;
399 
400 		/* Zero byte transfers aren't allowed. */
401 		if (sc->sc_buffer == NULL || sc->sc_buffer->buf == NULL ||
402 		    sc->sc_buffer->len == 0) {
403 			err = EINVAL;
404 			break;
405 		}
406 
407 		/* Check if the i2c bus is free. */
408 		if (repstart == 0) {
409 			/*
410 			 * On repeated start we send the START condition while
411 			 * the bus _is_ busy.
412 			 */
413 			timeout = 0;
414 			while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
415 				if (timeout++ > 100) {
416 					err = EBUSY;
417 					goto out;
418 				}
419 				DELAY(1000);
420 			}
421 			timeout = 0;
422 		} else
423 			repstart = 0;
424 
425 		if (sc->sc_buffer->flags & IIC_M_NOSTOP)
426 			repstart = 1;
427 
428 		/* Set the slave address. */
429 		ti_i2c_write_2(sc, I2C_REG_SA, msgs[i].slave >> 1);
430 
431 		/* Write the data length. */
432 		ti_i2c_write_2(sc, I2C_REG_CNT, sc->sc_buffer->len);
433 
434 		/* Clear the RX and the TX FIFO. */
435 		reg = ti_i2c_read_2(sc, I2C_REG_BUF);
436 		reg |= I2C_BUF_RXFIFO_CLR | I2C_BUF_TXFIFO_CLR;
437 		ti_i2c_write_2(sc, I2C_REG_BUF, reg);
438 
439 		reg = sc->sc_con_reg | I2C_CON_STT;
440 		if (repstart == 0)
441 			reg |= I2C_CON_STP;
442 		if ((sc->sc_buffer->flags & IIC_M_RD) == 0)
443 			reg |= I2C_CON_TRX;
444 		ti_i2c_write_2(sc, I2C_REG_CON, reg);
445 
446 		/* Wait for an event. */
447 		err = mtx_sleep(sc, &sc->sc_mtx, 0, "i2ciowait", hz);
448 		if (err == 0)
449 			err = sc->sc_error;
450 
451 		if (err)
452 			break;
453 	}
454 
455 out:
456 	if (timeout == 0) {
457 		while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
458 			if (timeout++ > 100)
459 				break;
460 			DELAY(1000);
461 		}
462 	}
463 	/* Put the controller in master mode again. */
464 	if ((ti_i2c_read_2(sc, I2C_REG_CON) & I2C_CON_MST) == 0)
465 		ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
466 
467 	sc->sc_buffer = NULL;
468 	sc->sc_bus_inuse = 0;
469 
470 	/* Wake up the processes that are waiting for the bus. */
471 	wakeup(sc);
472 
473 	TI_I2C_UNLOCK(sc);
474 
475 	return (err);
476 }
477 
478 /**
479  *	ti_i2c_callback - as we only provide iicbus_transfer() interface
480  * 		we don't need to implement the serialization here.
481  *	@dev: i2c device handle
482  *
483  *
484  *
485  *	LOCKING:
486  *	Called from timer context
487  *
488  *	RETURNS:
489  *	EH_HANDLED or EH_NOT_HANDLED
490  */
491 static int
492 ti_i2c_callback(device_t dev, int index, caddr_t data)
493 {
494 	int error = 0;
495 
496 	switch (index) {
497 		case IIC_REQUEST_BUS:
498 			break;
499 
500 		case IIC_RELEASE_BUS:
501 			break;
502 
503 		default:
504 			error = EINVAL;
505 	}
506 
507 	return (error);
508 }
509 
510 static int
511 ti_i2c_reset(struct ti_i2c_softc *sc, u_char speed)
512 {
513 	int timeout;
514 	struct ti_i2c_clock_config *clkcfg;
515 	uint16_t fifo_trsh, reg, scll, sclh;
516 
517 	switch (ti_chip()) {
518 #ifdef SOC_OMAP4
519 	case CHIP_OMAP_4:
520 		clkcfg = ti_omap4_i2c_clock_configs;
521 		break;
522 #endif
523 #ifdef SOC_TI_AM335X
524 	case CHIP_AM335X:
525 		clkcfg = ti_am335x_i2c_clock_configs;
526 		break;
527 #endif
528 	default:
529 		panic("Unknown Ti SoC, unable to reset the i2c");
530 	}
531 	while (clkcfg->speed != -1) {
532 		if (clkcfg->speed == speed)
533 			break;
534 		clkcfg++;
535 	}
536 	if (clkcfg->speed == -1)
537 		return (EINVAL);
538 
539 	/*
540 	 * 23.1.4.3 - HS I2C Software Reset
541 	 *    From OMAP4 TRM at page 4068.
542 	 *
543 	 * 1. Ensure that the module is disabled.
544 	 */
545 	sc->sc_con_reg = 0;
546 	ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
547 
548 	/* 2. Issue a softreset to the controller. */
549 	bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, I2C_REG_SYSC_SRST);
550 
551 	/*
552 	 * 3. Enable the module.
553 	 *    The I2Ci.I2C_SYSS[0] RDONE bit is asserted only after the module
554 	 *    is enabled by setting the I2Ci.I2C_CON[15] I2C_EN bit to 1.
555 	 */
556 	ti_i2c_write_2(sc, I2C_REG_CON, I2C_CON_I2C_EN);
557 
558  	/* 4. Wait for the software reset to complete. */
559 	timeout = 0;
560 	while ((ti_i2c_read_2(sc, I2C_REG_SYSS) & I2C_SYSS_RDONE) == 0) {
561 		if (timeout++ > 100)
562 			return (EBUSY);
563 		DELAY(100);
564 	}
565 
566 	/*
567 	 * Disable the I2C controller once again, now that the reset has
568 	 * finished.
569 	 */
570 	ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
571 
572 	/*
573 	 * The following sequence is taken from the OMAP4 TRM at page 4077.
574 	 *
575 	 * 1. Enable the functional and interface clocks (see Section
576 	 *    23.1.5.1.1.1.1).  Done at ti_i2c_activate().
577 	 *
578 	 * 2. Program the prescaler to obtain an approximately 12MHz internal
579 	 *    sampling clock (I2Ci_INTERNAL_CLK) by programming the
580 	 *    corresponding value in the I2Ci.I2C_PSC[3:0] PSC field.
581 	 *    This value depends on the frequency of the functional clock
582 	 *    (I2Ci_FCLK).  Because this frequency is 96MHz, the
583 	 *    I2Ci.I2C_PSC[7:0] PSC field value is 0x7.
584 	 */
585 	ti_i2c_write_2(sc, I2C_REG_PSC, clkcfg->psc);
586 
587 	/*
588 	 * 3. Program the I2Ci.I2C_SCLL[7:0] SCLL and I2Ci.I2C_SCLH[7:0] SCLH
589 	 *    bit fields to obtain a bit rate of 100 Kbps, 400 Kbps or 1Mbps.
590 	 *    These values depend on the internal sampling clock frequency
591 	 *    (see Table 23-8).
592 	 */
593 	scll = clkcfg->scll & I2C_SCLL_MASK;
594 	sclh = clkcfg->sclh & I2C_SCLH_MASK;
595 
596 	/*
597 	 * 4. (Optional) Program the I2Ci.I2C_SCLL[15:8] HSSCLL and
598 	 *    I2Ci.I2C_SCLH[15:8] HSSCLH fields to obtain a bit rate of
599 	 *    400K bps or 3.4M bps (for the second phase of HS mode).  These
600 	 *    values depend on the internal sampling clock frequency (see
601 	 *    Table 23-8).
602 	 *
603 	 * 5. (Optional) If a bit rate of 3.4M bps is used and the bus line
604 	 *    capacitance exceeds 45 pF, (see Section 18.4.8, PAD Functional
605 	 *    Multiplexing and Configuration).
606 	 */
607 	switch (ti_chip()) {
608 #ifdef SOC_OMAP4
609 	case CHIP_OMAP_4:
610 		if ((clkcfg->hsscll + clkcfg->hssclh) > 0) {
611 			scll |= clkcfg->hsscll << I2C_HSSCLL_SHIFT;
612 			sclh |= clkcfg->hssclh << I2C_HSSCLH_SHIFT;
613 			sc->sc_con_reg |= I2C_CON_OPMODE_HS;
614 		}
615 		break;
616 #endif
617 	}
618 
619 	/* Write the selected bit rate. */
620 	ti_i2c_write_2(sc, I2C_REG_SCLL, scll);
621 	ti_i2c_write_2(sc, I2C_REG_SCLH, sclh);
622 
623 	/*
624 	 * 6. Configure the Own Address of the I2C controller by storing it in
625 	 *    the I2Ci.I2C_OA0 register.  Up to four Own Addresses can be
626 	 *    programmed in the I2Ci.I2C_OAi registers (where i = 0, 1, 2, 3)
627 	 *    for each I2C controller.
628 	 *
629 	 * Note: For a 10-bit address, set the corresponding expand Own Address
630 	 * bit in the I2Ci.I2C_CON register.
631 	 *
632 	 * Driver currently always in single master mode so ignore this step.
633 	 */
634 
635 	/*
636 	 * 7. Set the TX threshold (in transmitter mode) and the RX threshold
637 	 *    (in receiver mode) by setting the I2Ci.I2C_BUF[5:0]XTRSH field to
638 	 *    (TX threshold - 1) and the I2Ci.I2C_BUF[13:8]RTRSH field to (RX
639 	 *    threshold - 1), where the TX and RX thresholds are greater than
640 	 *    or equal to 1.
641 	 *
642 	 * The threshold is set to 5 for now.
643 	 */
644 	fifo_trsh = (sc->sc_fifo_trsh - 1) & I2C_BUF_TRSH_MASK;
645 	reg = fifo_trsh | (fifo_trsh << I2C_BUF_RXTRSH_SHIFT);
646 	ti_i2c_write_2(sc, I2C_REG_BUF, reg);
647 
648 	/*
649 	 * 8. Take the I2C controller out of reset by setting the
650 	 *    I2Ci.I2C_CON[15] I2C_EN bit to 1.
651 	 *
652 	 * 23.1.5.1.1.1.2 - Initialize the I2C Controller
653 	 *
654 	 * To initialize the I2C controller, perform the following steps:
655 	 *
656 	 * 1. Configure the I2Ci.I2C_CON register:
657 	 *     . For master or slave mode, set the I2Ci.I2C_CON[10] MST bit
658 	 *       (0: slave, 1: master).
659 	 *     . For transmitter or receiver mode, set the I2Ci.I2C_CON[9] TRX
660 	 *       bit (0: receiver, 1: transmitter).
661 	 */
662 
663 	/* Enable the I2C controller in master mode. */
664 	sc->sc_con_reg |= I2C_CON_I2C_EN | I2C_CON_MST;
665 	ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
666 
667 	/*
668 	 * 2. If using an interrupt to transmit/receive data, set the
669 	 *    corresponding bit in the I2Ci.I2C_IE register (the I2Ci.I2C_IE[4]
670 	 *    XRDY_IE bit for the transmit interrupt, the I2Ci.I2C_IE[3] RRDY
671 	 *    bit for the receive interrupt).
672 	 */
673 
674 	/* Set the interrupts we want to be notified. */
675 	reg = I2C_IE_XDR |	/* Transmit draining interrupt. */
676 	    I2C_IE_XRDY |	/* Transmit Data Ready interrupt. */
677 	    I2C_IE_RDR |	/* Receive draining interrupt. */
678 	    I2C_IE_RRDY |	/* Receive Data Ready interrupt. */
679 	    I2C_IE_ARDY |	/* Register Access Ready interrupt. */
680 	    I2C_IE_NACK |	/* No Acknowledgment interrupt. */
681 	    I2C_IE_AL;		/* Arbitration lost interrupt. */
682 
683 	/* Enable the interrupts. */
684 	ti_i2c_write_2(sc, I2C_REG_IRQENABLE_SET, reg);
685 
686 	/*
687 	 * 3. If using DMA to receive/transmit data, set to 1 the corresponding
688 	 *    bit in the I2Ci.I2C_BUF register (the I2Ci.I2C_BUF[15] RDMA_EN
689 	 *    bit for the receive DMA channel, the I2Ci.I2C_BUF[7] XDMA_EN bit
690 	 *    for the transmit DMA channel).
691 	 *
692 	 * Not using DMA for now, so ignore this.
693 	 */
694 
695 	return (0);
696 }
697 
698 static int
699 ti_i2c_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
700 {
701 	struct ti_i2c_softc *sc;
702 	int err;
703 
704 	sc = device_get_softc(dev);
705 	TI_I2C_LOCK(sc);
706 	err = ti_i2c_reset(sc, speed);
707 	TI_I2C_UNLOCK(sc);
708 	if (err)
709 		return (err);
710 
711 	return (IIC_ENOADDR);
712 }
713 
714 static int
715 ti_i2c_activate(device_t dev)
716 {
717 	clk_ident_t clk;
718 	int err;
719 	struct ti_i2c_softc *sc;
720 
721 	sc = (struct ti_i2c_softc*)device_get_softc(dev);
722 
723 	/*
724 	 * 1. Enable the functional and interface clocks (see Section
725 	 * 23.1.5.1.1.1.1).
726 	 */
727 	clk = I2C0_CLK + sc->device_id;
728 	err = ti_prcm_clk_enable(clk);
729 	if (err)
730 		return (err);
731 
732 	return (ti_i2c_reset(sc, IIC_UNKNOWN));
733 }
734 
735 /**
736  *	ti_i2c_deactivate - deactivates the controller and releases resources
737  *	@dev: i2c device handle
738  *
739  *
740  *
741  *	LOCKING:
742  *	Assumed called in an atomic context.
743  *
744  *	RETURNS:
745  *	nothing
746  */
747 static void
748 ti_i2c_deactivate(device_t dev)
749 {
750 	struct ti_i2c_softc *sc = device_get_softc(dev);
751 	clk_ident_t clk;
752 
753 	/* Disable the controller - cancel all transactions. */
754 	ti_i2c_write_2(sc, I2C_REG_IRQENABLE_CLR, 0xffff);
755 	ti_i2c_write_2(sc, I2C_REG_STATUS, 0xffff);
756 	ti_i2c_write_2(sc, I2C_REG_CON, 0);
757 
758 	/* Release the interrupt handler. */
759 	if (sc->sc_irq_h != NULL) {
760 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
761 		sc->sc_irq_h = NULL;
762 	}
763 
764 	bus_generic_detach(sc->sc_dev);
765 
766 	/* Unmap the I2C controller registers. */
767 	if (sc->sc_mem_res != NULL) {
768 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
769 		sc->sc_mem_res = NULL;
770 	}
771 
772 	/* Release the IRQ resource. */
773 	if (sc->sc_irq_res != NULL) {
774 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
775 		sc->sc_irq_res = NULL;
776 	}
777 
778 	/* Finally disable the functional and interface clocks. */
779 	clk = I2C0_CLK + sc->device_id;
780 	ti_prcm_clk_disable(clk);
781 }
782 
783 static int
784 ti_i2c_sysctl_clk(SYSCTL_HANDLER_ARGS)
785 {
786 	device_t dev;
787 	int clk, psc, sclh, scll;
788 	struct ti_i2c_softc *sc;
789 
790 	dev = (device_t)arg1;
791 	sc = device_get_softc(dev);
792 
793 	TI_I2C_LOCK(sc);
794 	/* Get the system prescaler value. */
795 	psc = (int)ti_i2c_read_2(sc, I2C_REG_PSC) + 1;
796 
797 	/* Get the bitrate. */
798 	scll = (int)ti_i2c_read_2(sc, I2C_REG_SCLL) & I2C_SCLL_MASK;
799 	sclh = (int)ti_i2c_read_2(sc, I2C_REG_SCLH) & I2C_SCLH_MASK;
800 
801 	clk = I2C_CLK / psc / (scll + 7 + sclh + 5);
802 	TI_I2C_UNLOCK(sc);
803 
804 	return (sysctl_handle_int(oidp, &clk, 0, req));
805 }
806 
807 static int
808 ti_i2c_probe(device_t dev)
809 {
810 
811 	if (!ofw_bus_status_okay(dev))
812 		return (ENXIO);
813 	if (!ofw_bus_is_compatible(dev, "ti,i2c"))
814 		return (ENXIO);
815 	device_set_desc(dev, "TI I2C Controller");
816 
817 	return (0);
818 }
819 
820 static int
821 ti_i2c_attach(device_t dev)
822 {
823 	int err, rid;
824 	phandle_t node;
825 	struct ti_i2c_softc *sc;
826 	struct sysctl_ctx_list *ctx;
827 	struct sysctl_oid_list *tree;
828 	uint16_t fifosz;
829 
830  	sc = device_get_softc(dev);
831 	sc->sc_dev = dev;
832 
833 	/* Get the i2c device id from FDT. */
834 	node = ofw_bus_get_node(dev);
835 	if ((OF_getencprop(node, "i2c-device-id", &sc->device_id,
836 	    sizeof(sc->device_id))) <= 0) {
837 		device_printf(dev, "missing i2c-device-id attribute in FDT\n");
838 		return (ENXIO);
839 	}
840 
841 	/* Get the memory resource for the register mapping. */
842 	rid = 0;
843 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
844 	    RF_ACTIVE);
845 	if (sc->sc_mem_res == NULL) {
846 		device_printf(dev, "Cannot map registers.\n");
847 		return (ENXIO);
848 	}
849 
850 	/* Allocate our IRQ resource. */
851 	rid = 0;
852 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
853 	    RF_ACTIVE | RF_SHAREABLE);
854 	if (sc->sc_irq_res == NULL) {
855 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
856 		device_printf(dev, "Cannot allocate interrupt.\n");
857 		return (ENXIO);
858 	}
859 
860 	TI_I2C_LOCK_INIT(sc);
861 
862 	/* First of all, we _must_ activate the H/W. */
863 	err = ti_i2c_activate(dev);
864 	if (err) {
865 		device_printf(dev, "ti_i2c_activate failed\n");
866 		goto out;
867 	}
868 
869 	/* Read the version number of the I2C module */
870 	sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff;
871 
872 	/* Get the fifo size. */
873 	fifosz = ti_i2c_read_2(sc, I2C_REG_BUFSTAT);
874 	fifosz >>= I2C_BUFSTAT_FIFODEPTH_SHIFT;
875 	fifosz &= I2C_BUFSTAT_FIFODEPTH_MASK;
876 
877 	device_printf(dev, "I2C revision %d.%d FIFO size: %d bytes\n",
878 	    sc->sc_rev >> 4, sc->sc_rev & 0xf, 8 << fifosz);
879 
880 	/* Set the FIFO threshold to 5 for now. */
881 	sc->sc_fifo_trsh = 5;
882 
883 	ctx = device_get_sysctl_ctx(dev);
884 	tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
885 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_clock",
886 	    CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
887 	    ti_i2c_sysctl_clk, "IU", "I2C bus clock");
888 
889 	/* Activate the interrupt. */
890 	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
891 	    NULL, ti_i2c_intr, sc, &sc->sc_irq_h);
892 	if (err)
893 		goto out;
894 
895 	/* Attach the iicbus. */
896 	if ((sc->sc_iicbus = device_add_child(dev, "iicbus", -1)) == NULL) {
897 		device_printf(dev, "could not allocate iicbus instance\n");
898 		err = ENXIO;
899 		goto out;
900 	}
901 
902 	/* Probe and attach the iicbus */
903 	bus_generic_attach(dev);
904 
905 out:
906 	if (err) {
907 		ti_i2c_deactivate(dev);
908 		TI_I2C_LOCK_DESTROY(sc);
909 	}
910 
911 	return (err);
912 }
913 
914 static int
915 ti_i2c_detach(device_t dev)
916 {
917 	struct ti_i2c_softc *sc;
918 	int rv;
919 
920  	sc = device_get_softc(dev);
921 	ti_i2c_deactivate(dev);
922 	TI_I2C_LOCK_DESTROY(sc);
923 	if (sc->sc_iicbus &&
924 	    (rv = device_delete_child(dev, sc->sc_iicbus)) != 0)
925 		return (rv);
926 
927 	return (0);
928 }
929 
930 static phandle_t
931 ti_i2c_get_node(device_t bus, device_t dev)
932 {
933 
934 	/* Share controller node with iibus device. */
935 	return (ofw_bus_get_node(bus));
936 }
937 
938 static device_method_t ti_i2c_methods[] = {
939 	/* Device interface */
940 	DEVMETHOD(device_probe,		ti_i2c_probe),
941 	DEVMETHOD(device_attach,	ti_i2c_attach),
942 	DEVMETHOD(device_detach,	ti_i2c_detach),
943 
944 	/* OFW methods */
945 	DEVMETHOD(ofw_bus_get_node,	ti_i2c_get_node),
946 
947 	/* iicbus interface */
948 	DEVMETHOD(iicbus_callback,	ti_i2c_callback),
949 	DEVMETHOD(iicbus_reset,		ti_i2c_iicbus_reset),
950 	DEVMETHOD(iicbus_transfer,	ti_i2c_transfer),
951 
952 	DEVMETHOD_END
953 };
954 
955 static driver_t ti_i2c_driver = {
956 	"iichb",
957 	ti_i2c_methods,
958 	sizeof(struct ti_i2c_softc),
959 };
960 
961 static devclass_t ti_i2c_devclass;
962 
963 DRIVER_MODULE(ti_iic, simplebus, ti_i2c_driver, ti_i2c_devclass, 0, 0);
964 DRIVER_MODULE(iicbus, ti_iic, iicbus_driver, iicbus_devclass, 0, 0);
965 
966 MODULE_DEPEND(ti_iic, ti_prcm, 1, 1, 1);
967 MODULE_DEPEND(ti_iic, iicbus, 1, 1, 1);
968