xref: /openbmc/linux/drivers/i2c/busses/i2c-iop3xx.c (revision 8f9082c5ce0e2c2f7ad0211b0c089f680d2efc11)
11da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
21da177e4SLinus Torvalds /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x       */
31da177e4SLinus Torvalds /* ------------------------------------------------------------------------- */
41da177e4SLinus Torvalds /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
51da177e4SLinus Torvalds  *                    <Peter dot Milne at D hyphen TACQ dot com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * With acknowledgements to i2c-algo-ibm_ocp.c by
81da177e4SLinus Torvalds  * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
131da177e4SLinus Torvalds  *
14fbd9a6d7SDan Williams  * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
151da177e4SLinus Torvalds  * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
201da177e4SLinus Torvalds  * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
211da177e4SLinus Torvalds  * - Make it work with IXP46x chips
221da177e4SLinus Torvalds  * - Cleanup function names, coding style, etc
231da177e4SLinus Torvalds  *
2439288e1aSPeter Milne  * - writing to slave address causes latchup on iop331.
2539288e1aSPeter Milne  *	fix: driver refuses to address self.
2639288e1aSPeter Milne  *
271da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
281da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
291da177e4SLinus Torvalds  * the Free Software Foundation, version 2.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/interrupt.h>
331da177e4SLinus Torvalds #include <linux/kernel.h>
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/delay.h>
361da177e4SLinus Torvalds #include <linux/slab.h>
371da177e4SLinus Torvalds #include <linux/init.h>
381da177e4SLinus Torvalds #include <linux/errno.h>
391da177e4SLinus Torvalds #include <linux/sched.h>
40d052d1beSRussell King #include <linux/platform_device.h>
411da177e4SLinus Torvalds #include <linux/i2c.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <asm/io.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include "i2c-iop3xx.h"
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* global unit counter */
4860507095SJean Delvare static int i2c_id;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static inline unsigned char
511da177e4SLinus Torvalds iic_cook_addr(struct i2c_msg *msg)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	unsigned char addr;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	addr = (msg->addr << 1);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	if (msg->flags & I2C_M_RD)
581da177e4SLinus Torvalds 		addr |= 1;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	/*
611da177e4SLinus Torvalds 	 * Read or Write?
621da177e4SLinus Torvalds 	 */
631da177e4SLinus Torvalds 	if (msg->flags & I2C_M_REV_DIR_ADDR)
641da177e4SLinus Torvalds 		addr ^= 1;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	return addr;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static void
701da177e4SLinus Torvalds iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	/* Follows devman 9.3 */
731da177e4SLinus Torvalds 	__raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
741da177e4SLinus Torvalds 	__raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
751da177e4SLinus Torvalds 	__raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static void
791da177e4SLinus Torvalds iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	/*
841da177e4SLinus Torvalds 	 * Every time unit enable is asserted, GPOD needs to be cleared
851da177e4SLinus Torvalds 	 * on IOP321 to avoid data corruption on the bus.
861da177e4SLinus Torvalds 	 */
871da177e4SLinus Torvalds #ifdef CONFIG_ARCH_IOP321
881da177e4SLinus Torvalds #define IOP321_GPOD_I2C0    0x00c0  /* clear these bits to enable ch0 */
891da177e4SLinus Torvalds #define IOP321_GPOD_I2C1    0x0030  /* clear these bits to enable ch1 */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	*IOP321_GPOD &= (iop3xx_adap->id == 0) ? ~IOP321_GPOD_I2C0 :
921da177e4SLinus Torvalds 		~IOP321_GPOD_I2C1;
931da177e4SLinus Torvalds #endif
941da177e4SLinus Torvalds 	/* NB SR bits not same position as CR IE bits :-( */
951da177e4SLinus Torvalds 	iop3xx_adap->SR_enabled =
961da177e4SLinus Torvalds 		IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
971da177e4SLinus Torvalds 		IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
1001da177e4SLinus Torvalds 		IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static void
1061da177e4SLinus Torvalds iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
1111da177e4SLinus Torvalds 		IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * NB: the handler has to clear the source of the interrupt!
1181da177e4SLinus Torvalds  * Then it passes the SR flags of interest to BH via adap data
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds static irqreturn_t
1211da177e4SLinus Torvalds iop3xx_i2c_irq_handler(int this_irq, void *dev_id, struct pt_regs *regs)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
1241da177e4SLinus Torvalds 	u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if ((sr &= iop3xx_adap->SR_enabled)) {
1271da177e4SLinus Torvalds 		__raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
1281da177e4SLinus Torvalds 		iop3xx_adap->SR_received |= sr;
1291da177e4SLinus Torvalds 		wake_up_interruptible(&iop3xx_adap->waitq);
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 	return IRQ_HANDLED;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* check all error conditions, clear them , report most important */
1351da177e4SLinus Torvalds static int
1361da177e4SLinus Torvalds iop3xx_i2c_error(u32 sr)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	int rc = 0;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	if ((sr & IOP3XX_ISR_BERRD)) {
1411da177e4SLinus Torvalds 		if ( !rc ) rc = -I2C_ERR_BERR;
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 	if ((sr & IOP3XX_ISR_ALD)) {
1441da177e4SLinus Torvalds 		if ( !rc ) rc = -I2C_ERR_ALD;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 	return rc;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static inline u32
1501da177e4SLinus Torvalds iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	unsigned long flags;
1531da177e4SLinus Torvalds 	u32 sr;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	spin_lock_irqsave(&iop3xx_adap->lock, flags);
1561da177e4SLinus Torvalds 	sr = iop3xx_adap->SR_received;
1571da177e4SLinus Torvalds 	iop3xx_adap->SR_received = 0;
1581da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	return sr;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds  * sleep until interrupted, then recover and analyse the SR
1651da177e4SLinus Torvalds  * saved by handler
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds typedef int (* compare_func)(unsigned test, unsigned mask);
1681da177e4SLinus Torvalds /* returns 1 on correct comparison */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static int
1711da177e4SLinus Torvalds iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
1721da177e4SLinus Torvalds 			  unsigned flags, unsigned* status,
1731da177e4SLinus Torvalds 			  compare_func compare)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	unsigned sr = 0;
1761da177e4SLinus Torvalds 	int interrupted;
1771da177e4SLinus Torvalds 	int done;
1781da177e4SLinus Torvalds 	int rc = 0;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	do {
1811da177e4SLinus Torvalds 		interrupted = wait_event_interruptible_timeout (
1821da177e4SLinus Torvalds 			iop3xx_adap->waitq,
1831da177e4SLinus Torvalds 			(done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
1841da177e4SLinus Torvalds 			1 * HZ;
1851da177e4SLinus Torvalds 			);
1861da177e4SLinus Torvalds 		if ((rc = iop3xx_i2c_error(sr)) < 0) {
1871da177e4SLinus Torvalds 			*status = sr;
1881da177e4SLinus Torvalds 			return rc;
1891da177e4SLinus Torvalds 		} else if (!interrupted) {
1901da177e4SLinus Torvalds 			*status = sr;
1911da177e4SLinus Torvalds 			return -ETIMEDOUT;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	} while(!done);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	*status = sr;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	return 0;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * Concrete compare_funcs
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds static int
2041da177e4SLinus Torvalds all_bits_clear(unsigned test, unsigned mask)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	return (test & mask) == 0;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds static int
2101da177e4SLinus Torvalds any_bits_set(unsigned test, unsigned mask)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	return (test & mask) != 0;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds static int
2161da177e4SLinus Torvalds iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2191da177e4SLinus Torvalds 		iop3xx_adap,
2201da177e4SLinus Torvalds 	        IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2211da177e4SLinus Torvalds 		status, any_bits_set);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static int
2251da177e4SLinus Torvalds iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2281da177e4SLinus Torvalds 		iop3xx_adap,
2291da177e4SLinus Torvalds 		IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
2301da177e4SLinus Torvalds 		status,	any_bits_set);
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static int
2341da177e4SLinus Torvalds iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	return iop3xx_i2c_wait_event(
2371da177e4SLinus Torvalds 		iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static int
2411da177e4SLinus Torvalds iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
2421da177e4SLinus Torvalds 				struct i2c_msg* msg)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2451da177e4SLinus Torvalds 	int status;
2461da177e4SLinus Torvalds 	int rc;
2471da177e4SLinus Torvalds 
24839288e1aSPeter Milne 	/* avoid writing to my slave address (hangs on 80331),
24939288e1aSPeter Milne 	 * forbidden in Intel developer manual
25039288e1aSPeter Milne 	 */
25139288e1aSPeter Milne 	if (msg->addr == MYSAR) {
25239288e1aSPeter Milne 		return -EBUSY;
25339288e1aSPeter Milne 	}
25439288e1aSPeter Milne 
2551da177e4SLinus Torvalds 	__raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
2581da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2611da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	return rc;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static int
2671da177e4SLinus Torvalds iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
2681da177e4SLinus Torvalds 				int stop)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2711da177e4SLinus Torvalds 	int status;
2721da177e4SLinus Torvalds 	int rc = 0;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	__raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
2751da177e4SLinus Torvalds 	cr &= ~IOP3XX_ICR_MSTART;
2761da177e4SLinus Torvalds 	if (stop) {
2771da177e4SLinus Torvalds 		cr |= IOP3XX_ICR_MSTOP;
2781da177e4SLinus Torvalds 	} else {
2791da177e4SLinus Torvalds 		cr &= ~IOP3XX_ICR_MSTOP;
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_TBYTE;
2821da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
2831da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	return rc;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static int
2891da177e4SLinus Torvalds iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
2901da177e4SLinus Torvalds 				int stop)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
2931da177e4SLinus Torvalds 	int status;
2941da177e4SLinus Torvalds 	int rc = 0;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	cr &= ~IOP3XX_ICR_MSTART;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	if (stop) {
2991da177e4SLinus Torvalds 		cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
3001da177e4SLinus Torvalds 	} else {
3011da177e4SLinus Torvalds 		cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	cr |= IOP3XX_ICR_TBYTE;
3041da177e4SLinus Torvalds 	__raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	*byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	return rc;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds static int
3141da177e4SLinus Torvalds iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3171da177e4SLinus Torvalds 	int ii;
3181da177e4SLinus Torvalds 	int rc = 0;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	for (ii = 0; rc == 0 && ii != count; ++ii)
3211da177e4SLinus Torvalds 		rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
3221da177e4SLinus Torvalds 	return rc;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static int
3261da177e4SLinus Torvalds iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3291da177e4SLinus Torvalds 	int ii;
3301da177e4SLinus Torvalds 	int rc = 0;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	for (ii = 0; rc == 0 && ii != count; ++ii)
3331da177e4SLinus Torvalds 		rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return rc;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * Description:  This function implements combined transactions.  Combined
3401da177e4SLinus Torvalds  * transactions consist of combinations of reading and writing blocks of data.
3411da177e4SLinus Torvalds  * FROM THE SAME ADDRESS
3421da177e4SLinus Torvalds  * Each transfer (i.e. a read or a write) is separated by a repeated start
3431da177e4SLinus Torvalds  * condition.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds static int
3461da177e4SLinus Torvalds iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3491da177e4SLinus Torvalds 	int rc;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
3521da177e4SLinus Torvalds 	if (rc < 0) {
3531da177e4SLinus Torvalds 		return rc;
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	if ((pmsg->flags&I2C_M_RD)) {
3571da177e4SLinus Torvalds 		return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
3581da177e4SLinus Torvalds 	} else {
3591da177e4SLinus Torvalds 		return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * master_xfer() - main read/write entry
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds static int
3671da177e4SLinus Torvalds iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
3681da177e4SLinus Torvalds 				int num)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
3711da177e4SLinus Torvalds 	int im = 0;
3721da177e4SLinus Torvalds 	int ret = 0;
3731da177e4SLinus Torvalds 	int status;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	iop3xx_i2c_wait_idle(iop3xx_adap, &status);
3761da177e4SLinus Torvalds 	iop3xx_i2c_reset(iop3xx_adap);
3771da177e4SLinus Torvalds 	iop3xx_i2c_enable(iop3xx_adap);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	for (im = 0; ret == 0 && im != num; im++) {
3801da177e4SLinus Torvalds 		ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	iop3xx_i2c_transaction_cleanup(iop3xx_adap);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	if(ret)
3861da177e4SLinus Torvalds 		return ret;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	return im;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds static int
3921da177e4SLinus Torvalds iop3xx_i2c_algo_control(struct i2c_adapter *adapter, unsigned int cmd,
3931da177e4SLinus Torvalds 			unsigned long arg)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static u32
3991da177e4SLinus Torvalds iop3xx_i2c_func(struct i2c_adapter *adap)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
404*8f9082c5SJean Delvare static const struct i2c_algorithm iop3xx_i2c_algo = {
4051da177e4SLinus Torvalds 	.master_xfer	= iop3xx_i2c_master_xfer,
4061da177e4SLinus Torvalds 	.algo_control	= iop3xx_i2c_algo_control,
4071da177e4SLinus Torvalds 	.functionality	= iop3xx_i2c_func,
4081da177e4SLinus Torvalds };
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static int
4113ae5eaecSRussell King iop3xx_i2c_remove(struct platform_device *pdev)
4121da177e4SLinus Torvalds {
4133ae5eaecSRussell King 	struct i2c_adapter *padapter = platform_get_drvdata(pdev);
4141da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *adapter_data =
4151da177e4SLinus Torvalds 		(struct i2c_algo_iop3xx_data *)padapter->algo_data;
4161da177e4SLinus Torvalds 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4171da177e4SLinus Torvalds 	unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/*
4201da177e4SLinus Torvalds 	 * Disable the actual HW unit
4211da177e4SLinus Torvalds 	 */
4221da177e4SLinus Torvalds 	cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
4231da177e4SLinus Torvalds 		IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
4241da177e4SLinus Torvalds 	__raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	iounmap((void __iomem*)adapter_data->ioaddr);
4271da177e4SLinus Torvalds 	release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
4281da177e4SLinus Torvalds 	kfree(adapter_data);
4291da177e4SLinus Torvalds 	kfree(padapter);
4301da177e4SLinus Torvalds 
4313ae5eaecSRussell King 	platform_set_drvdata(pdev, NULL);
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	return 0;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds static int
4373ae5eaecSRussell King iop3xx_i2c_probe(struct platform_device *pdev)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	struct resource *res;
44048944738SDavid Vrabel 	int ret, irq;
4411da177e4SLinus Torvalds 	struct i2c_adapter *new_adapter;
4421da177e4SLinus Torvalds 	struct i2c_algo_iop3xx_data *adapter_data;
4431da177e4SLinus Torvalds 
4445263ebb5SDeepak Saxena 	new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
4451da177e4SLinus Torvalds 	if (!new_adapter) {
4461da177e4SLinus Torvalds 		ret = -ENOMEM;
4471da177e4SLinus Torvalds 		goto out;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
4505263ebb5SDeepak Saxena 	adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
4511da177e4SLinus Torvalds 	if (!adapter_data) {
4521da177e4SLinus Torvalds 		ret = -ENOMEM;
4531da177e4SLinus Torvalds 		goto free_adapter;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4571da177e4SLinus Torvalds 	if (!res) {
4581da177e4SLinus Torvalds 		ret = -ENODEV;
4591da177e4SLinus Torvalds 		goto free_both;
4601da177e4SLinus Torvalds 	}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
4631da177e4SLinus Torvalds 		ret = -EBUSY;
4641da177e4SLinus Torvalds 		goto free_both;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/* set the adapter enumeration # */
4681da177e4SLinus Torvalds 	adapter_data->id = i2c_id++;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
4711da177e4SLinus Torvalds 	if (!adapter_data->ioaddr) {
4721da177e4SLinus Torvalds 		ret = -ENOMEM;
4731da177e4SLinus Torvalds 		goto release_region;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
47648944738SDavid Vrabel 	irq = platform_get_irq(pdev, 0);
47748944738SDavid Vrabel 	if (irq < 0) {
47848944738SDavid Vrabel 		ret = -ENXIO;
47948944738SDavid Vrabel 		goto unmap;
48048944738SDavid Vrabel 	}
48148944738SDavid Vrabel 	ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
4821da177e4SLinus Torvalds 				pdev->name, adapter_data);
483fbd9a6d7SDan Williams 
484fbd9a6d7SDan Williams 	if (ret) {
4851da177e4SLinus Torvalds 		ret = -EIO;
4861da177e4SLinus Torvalds 		goto unmap;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
4901da177e4SLinus Torvalds 	new_adapter->id = I2C_HW_IOP3XX;
4911da177e4SLinus Torvalds 	new_adapter->owner = THIS_MODULE;
4921da177e4SLinus Torvalds 	new_adapter->dev.parent = &pdev->dev;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	/*
4951da177e4SLinus Torvalds 	 * Default values...should these come in from board code?
4961da177e4SLinus Torvalds 	 */
4971da177e4SLinus Torvalds 	new_adapter->timeout = 100;
4981da177e4SLinus Torvalds 	new_adapter->retries = 3;
4991da177e4SLinus Torvalds 	new_adapter->algo = &iop3xx_i2c_algo;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	init_waitqueue_head(&adapter_data->waitq);
5021da177e4SLinus Torvalds 	spin_lock_init(&adapter_data->lock);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	iop3xx_i2c_reset(adapter_data);
5051da177e4SLinus Torvalds 	iop3xx_i2c_enable(adapter_data);
5061da177e4SLinus Torvalds 
5073ae5eaecSRussell King 	platform_set_drvdata(pdev, new_adapter);
5081da177e4SLinus Torvalds 	new_adapter->algo_data = adapter_data;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	i2c_add_adapter(new_adapter);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	return 0;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds unmap:
5151da177e4SLinus Torvalds 	iounmap((void __iomem*)adapter_data->ioaddr);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds release_region:
5181da177e4SLinus Torvalds 	release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds free_both:
5211da177e4SLinus Torvalds 	kfree(adapter_data);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds free_adapter:
5241da177e4SLinus Torvalds 	kfree(new_adapter);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds out:
5271da177e4SLinus Torvalds 	return ret;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 
5313ae5eaecSRussell King static struct platform_driver iop3xx_i2c_driver = {
5323ae5eaecSRussell King 	.probe		= iop3xx_i2c_probe,
5333ae5eaecSRussell King 	.remove		= iop3xx_i2c_remove,
5343ae5eaecSRussell King 	.driver		= {
5350cf36281SLaurent Riffard 		.owner	= THIS_MODULE,
5361da177e4SLinus Torvalds 		.name	= "IOP3xx-I2C",
5373ae5eaecSRussell King 	},
5381da177e4SLinus Torvalds };
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static int __init
5411da177e4SLinus Torvalds i2c_iop3xx_init (void)
5421da177e4SLinus Torvalds {
5433ae5eaecSRussell King 	return platform_driver_register(&iop3xx_i2c_driver);
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds static void __exit
5471da177e4SLinus Torvalds i2c_iop3xx_exit (void)
5481da177e4SLinus Torvalds {
5493ae5eaecSRussell King 	platform_driver_unregister(&iop3xx_i2c_driver);
5501da177e4SLinus Torvalds 	return;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds module_init (i2c_iop3xx_init);
5541da177e4SLinus Torvalds module_exit (i2c_iop3xx_exit);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
5571da177e4SLinus Torvalds MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
5581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
559