mv_twsi.c revision 295567
1/*-
2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3 * All rights reserved.
4 *
5 * Developed by Semihalf.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of MARVELL nor the names of contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
34 * SoCs. Supports master operation only, and works in polling mode.
35 *
36 * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
37 * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sys/arm/mv/twsi.c 295567 2016-02-12 17:04:03Z andrew $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/bus.h>
46#include <sys/kernel.h>
47#include <sys/module.h>
48#include <sys/resource.h>
49
50#include <machine/_inttypes.h>
51#include <machine/bus.h>
52#include <machine/resource.h>
53
54#include <sys/rman.h>
55
56#include <sys/lock.h>
57#include <sys/mutex.h>
58
59#include <dev/iicbus/iiconf.h>
60#include <dev/iicbus/iicbus.h>
61#include <dev/fdt/fdt_common.h>
62#include <dev/ofw/ofw_bus.h>
63#include <dev/ofw/ofw_bus_subr.h>
64
65#include <arm/mv/mvreg.h>
66#include <arm/mv/mvvar.h>
67
68#include "iicbus_if.h"
69
70#define MV_TWSI_NAME		"twsi"
71#define	IICBUS_DEVNAME		"iicbus"
72
73#define TWSI_SLAVE_ADDR		0x00
74#define TWSI_EXT_SLAVE_ADDR	0x10
75#define TWSI_DATA		0x04
76
77#define TWSI_CONTROL		0x08
78#define TWSI_CONTROL_ACK	(1 << 2)
79#define TWSI_CONTROL_IFLG	(1 << 3)
80#define TWSI_CONTROL_STOP	(1 << 4)
81#define TWSI_CONTROL_START	(1 << 5)
82#define TWSI_CONTROL_TWSIEN	(1 << 6)
83#define TWSI_CONTROL_INTEN	(1 << 7)
84
85#define TWSI_STATUS			0x0c
86#define TWSI_STATUS_START		0x08
87#define TWSI_STATUS_RPTD_START		0x10
88#define TWSI_STATUS_ADDR_W_ACK		0x18
89#define TWSI_STATUS_DATA_WR_ACK		0x28
90#define TWSI_STATUS_ADDR_R_ACK		0x40
91#define TWSI_STATUS_DATA_RD_ACK		0x50
92#define TWSI_STATUS_DATA_RD_NOACK	0x58
93
94#define TWSI_BAUD_RATE		0x0c
95#define	TWSI_BAUD_RATE_PARAM(M,N)	((((M) << 3) | ((N) & 0x7)) & 0x7f)
96#define	TWSI_BAUD_RATE_RAW(C,M,N)	((C)/((10*(M+1))<<(N+1)))
97#define	TWSI_BAUD_RATE_SLOW		50000	/* 50kHz */
98#define	TWSI_BAUD_RATE_FAST		100000	/* 100kHz */
99
100#define TWSI_SOFT_RESET		0x1c
101
102#define TWSI_DEBUG
103#undef TWSI_DEBUG
104
105#ifdef  TWSI_DEBUG
106#define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
107#else
108#define debugf(fmt, args...)
109#endif
110
111struct mv_twsi_softc {
112	device_t	dev;
113	struct resource	*res[1];	/* SYS_RES_MEMORY */
114	struct mtx	mutex;
115	device_t	iicbus;
116};
117
118static struct mv_twsi_baud_rate {
119	uint32_t	raw;
120	int		param;
121	int		m;
122	int		n;
123} baud_rate[IIC_FASTEST + 1];
124
125static int mv_twsi_probe(device_t);
126static int mv_twsi_attach(device_t);
127static int mv_twsi_detach(device_t);
128
129static int mv_twsi_reset(device_t dev, u_char speed, u_char addr,
130    u_char *oldaddr);
131static int mv_twsi_repeated_start(device_t dev, u_char slave, int timeout);
132static int mv_twsi_start(device_t dev, u_char slave, int timeout);
133static int mv_twsi_stop(device_t dev);
134static int mv_twsi_read(device_t dev, char *buf, int len, int *read, int last,
135    int delay);
136static int mv_twsi_write(device_t dev, const char *buf, int len, int *sent,
137    int timeout);
138
139static struct resource_spec res_spec[] = {
140	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
141	{ -1, 0 }
142};
143
144static struct ofw_compat_data compat_data[] = {
145	{ "mrvl,twsi",			true },
146	{ "marvell,mv64xxx-i2c",	true },
147	{ NULL,				false }
148};
149
150static device_method_t mv_twsi_methods[] = {
151	/* device interface */
152	DEVMETHOD(device_probe,		mv_twsi_probe),
153	DEVMETHOD(device_attach,	mv_twsi_attach),
154	DEVMETHOD(device_detach,	mv_twsi_detach),
155
156	/* iicbus interface */
157	DEVMETHOD(iicbus_callback, iicbus_null_callback),
158	DEVMETHOD(iicbus_repeated_start, mv_twsi_repeated_start),
159	DEVMETHOD(iicbus_start,		mv_twsi_start),
160	DEVMETHOD(iicbus_stop,		mv_twsi_stop),
161	DEVMETHOD(iicbus_write,		mv_twsi_write),
162	DEVMETHOD(iicbus_read,		mv_twsi_read),
163	DEVMETHOD(iicbus_reset,		mv_twsi_reset),
164	DEVMETHOD(iicbus_transfer,	iicbus_transfer_gen),
165	{ 0, 0 }
166};
167
168static devclass_t mv_twsi_devclass;
169
170static driver_t mv_twsi_driver = {
171	MV_TWSI_NAME,
172	mv_twsi_methods,
173	sizeof(struct mv_twsi_softc),
174};
175
176DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
177DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
178MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
179
180static __inline uint32_t
181TWSI_READ(struct mv_twsi_softc *sc, bus_size_t off)
182{
183
184	return (bus_read_4(sc->res[0], off));
185}
186
187static __inline void
188TWSI_WRITE(struct mv_twsi_softc *sc, bus_size_t off, uint32_t val)
189{
190
191	bus_write_4(sc->res[0], off, val);
192}
193
194static __inline void
195twsi_control_clear(struct mv_twsi_softc *sc, uint32_t mask)
196{
197	uint32_t val;
198
199	val = TWSI_READ(sc, TWSI_CONTROL);
200	val &= ~mask;
201	TWSI_WRITE(sc, TWSI_CONTROL, val);
202}
203
204static __inline void
205twsi_control_set(struct mv_twsi_softc *sc, uint32_t mask)
206{
207	uint32_t val;
208
209	val = TWSI_READ(sc, TWSI_CONTROL);
210	val |= mask;
211	TWSI_WRITE(sc, TWSI_CONTROL, val);
212}
213
214static __inline void
215twsi_clear_iflg(struct mv_twsi_softc *sc)
216{
217
218	DELAY(1000);
219	twsi_control_clear(sc, TWSI_CONTROL_IFLG);
220	DELAY(1000);
221}
222
223
224/*
225 * timeout given in us
226 * returns
227 *   0 on sucessfull mask change
228 *   non-zero on timeout
229 */
230static int
231twsi_poll_ctrl(struct mv_twsi_softc *sc, int timeout, uint32_t mask)
232{
233
234	timeout /= 10;
235	while (!(TWSI_READ(sc, TWSI_CONTROL) & mask)) {
236		DELAY(10);
237		if (--timeout < 0)
238			return (timeout);
239	}
240	return (0);
241}
242
243
244/*
245 * 'timeout' is given in us. Note also that timeout handling is not exact --
246 * twsi_locked_start() total wait can be more than 2 x timeout
247 * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
248 * or TWSI_STATUS_RPTD_START
249 */
250static int
251twsi_locked_start(device_t dev, struct mv_twsi_softc *sc, int32_t mask,
252    u_char slave, int timeout)
253{
254	int read_access, iflg_set = 0;
255	uint32_t status;
256
257	mtx_assert(&sc->mutex, MA_OWNED);
258
259	if (mask == TWSI_STATUS_RPTD_START)
260		/* read IFLG to know if it should be cleared later; from NBSD */
261		iflg_set = TWSI_READ(sc, TWSI_CONTROL) & TWSI_CONTROL_IFLG;
262
263	twsi_control_set(sc, TWSI_CONTROL_START);
264
265	if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
266		debugf("IFLG set, clearing\n");
267		twsi_clear_iflg(sc);
268	}
269
270	/*
271	 * Without this delay we timeout checking IFLG if the timeout is 0.
272	 * NBSD driver always waits here too.
273	 */
274	DELAY(1000);
275
276	if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
277		debugf("timeout sending %sSTART condition\n",
278		    mask == TWSI_STATUS_START ? "" : "repeated ");
279		return (IIC_ETIMEOUT);
280	}
281
282	status = TWSI_READ(sc, TWSI_STATUS);
283	if (status != mask) {
284		debugf("wrong status (%02x) after sending %sSTART condition\n",
285		    status, mask == TWSI_STATUS_START ? "" : "repeated ");
286		return (IIC_ESTATUS);
287	}
288
289	TWSI_WRITE(sc, TWSI_DATA, slave);
290	DELAY(1000);
291	twsi_clear_iflg(sc);
292
293	if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
294		debugf("timeout sending slave address\n");
295		return (IIC_ETIMEOUT);
296	}
297
298	read_access = (slave & 0x1) ? 1 : 0;
299	status = TWSI_READ(sc, TWSI_STATUS);
300	if (status != (read_access ?
301	    TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
302		debugf("no ACK (status: %02x) after sending slave address\n",
303		    status);
304		return (IIC_ENOACK);
305	}
306
307	return (IIC_NOERR);
308}
309
310static int
311mv_twsi_probe(device_t dev)
312{
313
314	if (!ofw_bus_status_okay(dev))
315		return (ENXIO);
316
317	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
318		return (ENXIO);
319
320	device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
321	return (BUS_PROBE_DEFAULT);
322}
323
324#define	ABSSUB(a,b)	(((a) > (b)) ? (a) - (b) : (b) - (a))
325static void
326mv_twsi_cal_baud_rate(const uint32_t target, struct mv_twsi_baud_rate *rate)
327{
328	uint32_t clk, cur, diff, diff0;
329	int m, n, m0, n0;
330
331	/* Calculate baud rate. */
332	m0 = n0 = 4;	/* Default values on reset */
333	diff0 = 0xffffffff;
334	clk = get_tclk();
335
336	for (n = 0; n < 8; n++) {
337		for (m = 0; m < 16; m++) {
338			cur = TWSI_BAUD_RATE_RAW(clk,m,n);
339			diff = ABSSUB(target, cur);
340			if (diff < diff0) {
341				m0 = m;
342				n0 = n;
343				diff0 = diff;
344			}
345		}
346	}
347	rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
348	rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
349	rate->m = m0;
350	rate->n = n0;
351}
352
353static int
354mv_twsi_attach(device_t dev)
355{
356	struct mv_twsi_softc *sc;
357	phandle_t child, iicbusnode;
358	device_t childdev;
359	struct iicbus_ivar *devi;
360	char dname[32];	/* 32 is taken from struct u_device */
361	uint32_t paddr;
362	int len, error;
363
364	sc = device_get_softc(dev);
365	sc->dev = dev;
366	bzero(baud_rate, sizeof(baud_rate));
367
368	mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
369
370	/* Allocate IO resources */
371	if (bus_alloc_resources(dev, res_spec, sc->res)) {
372		device_printf(dev, "could not allocate resources\n");
373		mv_twsi_detach(dev);
374		return (ENXIO);
375	}
376
377	mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &baud_rate[IIC_SLOW]);
378	mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &baud_rate[IIC_FAST]);
379	if (bootverbose)
380		device_printf(dev, "calculated baud rates are:\n"
381		    " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
382		    " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
383		    baud_rate[IIC_SLOW].raw / 1000,
384		    baud_rate[IIC_SLOW].m,
385		    baud_rate[IIC_SLOW].n,
386		    baud_rate[IIC_FAST].raw / 1000,
387		    baud_rate[IIC_FAST].m,
388		    baud_rate[IIC_FAST].n);
389
390	sc->iicbus = device_add_child(dev, IICBUS_DEVNAME, -1);
391	if (sc->iicbus == NULL) {
392		device_printf(dev, "could not add iicbus child\n");
393		mv_twsi_detach(dev);
394		return (ENXIO);
395	}
396	/* Attach iicbus. */
397	bus_generic_attach(dev);
398
399	iicbusnode = 0;
400	/* Find iicbus as the child devices in the device tree. */
401	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
402	    child = OF_peer(child)) {
403		len = OF_getproplen(child, "model");
404		if (len <= 0 || len > sizeof(dname))
405			continue;
406		error = OF_getprop(child, "model", &dname, len);
407		if (error == -1)
408			continue;
409		len = strlen(dname);
410		if (len == strlen(IICBUS_DEVNAME) &&
411		    strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
412			iicbusnode = child;
413			break;
414		}
415	}
416	if (iicbusnode == 0)
417		goto attach_end;
418
419	/* Attach child devices onto iicbus. */
420	for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
421		/* Get slave address. */
422		error = OF_getprop(child, "i2c-address", &paddr, sizeof(paddr));
423		if (error == -1)
424			error = OF_getprop(child, "reg", &paddr, sizeof(paddr));
425		if (error == -1)
426			continue;
427
428		/* Get device driver name. */
429		len = OF_getproplen(child, "model");
430		if (len <= 0 || len > sizeof(dname))
431			continue;
432		OF_getprop(child, "model", &dname, len);
433
434		if (bootverbose)
435			device_printf(dev, "adding a device %s at %d.\n",
436			    dname, fdt32_to_cpu(paddr));
437		childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
438		devi = IICBUS_IVAR(childdev);
439		devi->addr = fdt32_to_cpu(paddr);
440	}
441
442attach_end:
443	bus_generic_attach(sc->iicbus);
444
445	return (0);
446}
447
448static int
449mv_twsi_detach(device_t dev)
450{
451	struct mv_twsi_softc *sc;
452	int rv;
453
454	sc = device_get_softc(dev);
455
456	if ((rv = bus_generic_detach(dev)) != 0)
457		return (rv);
458
459	if (sc->iicbus != NULL)
460		if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
461			return (rv);
462
463	bus_release_resources(dev, res_spec, sc->res);
464
465	mtx_destroy(&sc->mutex);
466	return (0);
467}
468
469/*
470 * Only slave mode supported, disregard [old]addr
471 */
472static int
473mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
474{
475	struct mv_twsi_softc *sc;
476	uint32_t param;
477
478	sc = device_get_softc(dev);
479
480	switch (speed) {
481	case IIC_SLOW:
482	case IIC_FAST:
483		param = baud_rate[speed].param;
484		break;
485	case IIC_FASTEST:
486	case IIC_UNKNOWN:
487	default:
488		param = baud_rate[IIC_FAST].param;
489		break;
490	}
491
492	mtx_lock(&sc->mutex);
493	TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
494	DELAY(2000);
495	TWSI_WRITE(sc, TWSI_BAUD_RATE, param);
496	TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
497	DELAY(1000);
498	mtx_unlock(&sc->mutex);
499
500	return (0);
501}
502
503/*
504 * timeout is given in us
505 */
506static int
507mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
508{
509	struct mv_twsi_softc *sc;
510	int rv;
511
512	sc = device_get_softc(dev);
513
514	mtx_lock(&sc->mutex);
515	rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
516	    timeout);
517	mtx_unlock(&sc->mutex);
518
519	if (rv) {
520		mv_twsi_stop(dev);
521		return (rv);
522	} else
523		return (IIC_NOERR);
524}
525
526/*
527 * timeout is given in us
528 */
529static int
530mv_twsi_start(device_t dev, u_char slave, int timeout)
531{
532	struct mv_twsi_softc *sc;
533	int rv;
534
535	sc = device_get_softc(dev);
536
537	mtx_lock(&sc->mutex);
538	rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
539	mtx_unlock(&sc->mutex);
540
541	if (rv) {
542		mv_twsi_stop(dev);
543		return (rv);
544	} else
545		return (IIC_NOERR);
546}
547
548static int
549mv_twsi_stop(device_t dev)
550{
551	struct mv_twsi_softc *sc;
552
553	sc = device_get_softc(dev);
554
555	mtx_lock(&sc->mutex);
556	twsi_control_set(sc, TWSI_CONTROL_STOP);
557	DELAY(1000);
558	twsi_clear_iflg(sc);
559	mtx_unlock(&sc->mutex);
560
561	return (IIC_NOERR);
562}
563
564static int
565mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
566{
567	struct mv_twsi_softc *sc;
568	uint32_t status;
569	int last_byte, rv;
570
571	sc = device_get_softc(dev);
572
573	mtx_lock(&sc->mutex);
574	*read = 0;
575	while (*read < len) {
576		/*
577		 * Check if we are reading last byte of the last buffer,
578		 * do not send ACK then, per I2C specs
579		 */
580		last_byte = ((*read == len - 1) && last) ? 1 : 0;
581		if (last_byte)
582			twsi_control_clear(sc, TWSI_CONTROL_ACK);
583		else
584			twsi_control_set(sc, TWSI_CONTROL_ACK);
585
586		DELAY (1000);
587		twsi_clear_iflg(sc);
588
589		if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
590			debugf("timeout reading data\n");
591			rv = IIC_ETIMEOUT;
592			goto out;
593		}
594
595		status = TWSI_READ(sc, TWSI_STATUS);
596		if (status != (last_byte ?
597		    TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
598			debugf("wrong status (%02x) while reading\n", status);
599			rv = IIC_ESTATUS;
600			goto out;
601		}
602
603		*buf++ = TWSI_READ(sc, TWSI_DATA);
604		(*read)++;
605	}
606	rv = IIC_NOERR;
607out:
608	mtx_unlock(&sc->mutex);
609	return (rv);
610}
611
612static int
613mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
614{
615	struct mv_twsi_softc *sc;
616	uint32_t status;
617	int rv;
618
619	sc = device_get_softc(dev);
620
621	mtx_lock(&sc->mutex);
622	*sent = 0;
623	while (*sent < len) {
624		TWSI_WRITE(sc, TWSI_DATA, *buf++);
625
626		twsi_clear_iflg(sc);
627		if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
628			debugf("timeout writing data\n");
629			rv = IIC_ETIMEOUT;
630			goto out;
631		}
632
633		status = TWSI_READ(sc, TWSI_STATUS);
634		if (status != TWSI_STATUS_DATA_WR_ACK) {
635			debugf("wrong status (%02x) while writing\n", status);
636			rv = IIC_ESTATUS;
637			goto out;
638		}
639		(*sent)++;
640	}
641	rv = IIC_NOERR;
642out:
643	mtx_unlock(&sc->mutex);
644	return (rv);
645}
646