i2c-au1550.c 9.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * i2c-au1550.c: SMBus (i2c) adapter for Alchemy PSC interface
 * Copyright (C) 2004 Embedded Edge, LLC <dan@embeddededge.com>
 *
 * 2.6 port by Matt Porter <mporter@kernel.crashing.org>
 *
 * The documentation describes this as an SMBus controller, but it doesn't
 * understand any of the SMBus protocol in hardware.  It's really an I2C
 * controller that could emulate most of the SMBus in software.
 *
 * This is just a skeleton adapter to use with the Au1550 PSC
 * algorithm.  It was developed for the Pb1550, but will work with
 * any Au1550 board that has a similar PSC configuration.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
33
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
34 35 36
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/i2c.h>
37
#include <linux/slab.h>
L
Linus Torvalds 已提交
38

M
Manuel Lauss 已提交
39
#include <asm/mach-au1x00/au1000.h>
L
Linus Torvalds 已提交
40 41
#include <asm/mach-au1x00/au1xxx_psc.h>

42 43 44 45 46 47 48 49 50 51
#define PSC_SEL		0x00
#define PSC_CTRL	0x04
#define PSC_SMBCFG	0x08
#define PSC_SMBMSK	0x0C
#define PSC_SMBPCR	0x10
#define PSC_SMBSTAT	0x14
#define PSC_SMBEVNT	0x18
#define PSC_SMBTXRX	0x1C
#define PSC_SMBTMR	0x20

52
struct i2c_au1550_data {
53
	void __iomem *psc_base;
54 55 56 57
	int	xfer_timeout;
	struct i2c_adapter adap;
	struct resource *ioarea;
};
L
Linus Torvalds 已提交
58

59
static inline void WR(struct i2c_au1550_data *a, int r, unsigned long v)
L
Linus Torvalds 已提交
60
{
61 62 63
	__raw_writel(v, a->psc_base + r);
	wmb();
}
L
Linus Torvalds 已提交
64

65 66 67 68
static inline unsigned long RD(struct i2c_au1550_data *a, int r)
{
	return __raw_readl(a->psc_base + r);
}
L
Linus Torvalds 已提交
69

70 71 72 73 74
static int wait_xfer_done(struct i2c_au1550_data *adap)
{
	int i;

	/* Wait for Tx Buffer Empty */
L
Linus Torvalds 已提交
75
	for (i = 0; i < adap->xfer_timeout; i++) {
76
		if (RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_TE)
L
Linus Torvalds 已提交
77
			return 0;
78

L
Linus Torvalds 已提交
79 80 81 82 83 84
		udelay(1);
	}

	return -ETIMEDOUT;
}

85
static int wait_ack(struct i2c_au1550_data *adap)
L
Linus Torvalds 已提交
86
{
87
	unsigned long stat;
L
Linus Torvalds 已提交
88 89 90 91

	if (wait_xfer_done(adap))
		return -ETIMEDOUT;

92
	stat = RD(adap, PSC_SMBEVNT);
L
Linus Torvalds 已提交
93 94 95 96 97 98
	if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
		return -ETIMEDOUT;

	return 0;
}

99
static int wait_master_done(struct i2c_au1550_data *adap)
L
Linus Torvalds 已提交
100
{
101
	int i;
L
Linus Torvalds 已提交
102

103
	/* Wait for Master Done. */
104
	for (i = 0; i < 2 * adap->xfer_timeout; i++) {
105
		if ((RD(adap, PSC_SMBEVNT) & PSC_SMBEVNT_MD) != 0)
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113
			return 0;
		udelay(1);
	}

	return -ETIMEDOUT;
}

static int
114
do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
L
Linus Torvalds 已提交
115
{
116
	unsigned long stat;
L
Linus Torvalds 已提交
117

118 119 120
	/* Reset the FIFOs, clear events. */
	stat = RD(adap, PSC_SMBSTAT);
	WR(adap, PSC_SMBEVNT, PSC_SMBEVNT_ALLCLR);
D
Domen Puncer 已提交
121 122

	if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
123 124 125
		WR(adap, PSC_SMBPCR, PSC_SMBPCR_DC);
		while ((RD(adap, PSC_SMBPCR) & PSC_SMBPCR_DC) != 0)
			cpu_relax();
D
Domen Puncer 已提交
126 127
		udelay(50);
	}
L
Linus Torvalds 已提交
128

129
	/* Write out the i2c chip address and specify operation */
L
Linus Torvalds 已提交
130 131 132 133
	addr <<= 1;
	if (rd)
		addr |= 1;

134 135 136 137
	/* zero-byte xfers stop immediately */
	if (q)
		addr |= PSC_SMBTXRX_STP;

138 139 140
	/* Put byte into fifo, start up master. */
	WR(adap, PSC_SMBTXRX, addr);
	WR(adap, PSC_SMBPCR, PSC_SMBPCR_MS);
L
Linus Torvalds 已提交
141 142
	if (wait_ack(adap))
		return -EIO;
143
	return (q) ? wait_master_done(adap) : 0;
L
Linus Torvalds 已提交
144 145
}

146
static int wait_for_rx_byte(struct i2c_au1550_data *adap, unsigned char *out)
L
Linus Torvalds 已提交
147
{
148
	int j;
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158

	if (wait_xfer_done(adap))
		return -EIO;

	j =  adap->xfer_timeout * 100;
	do {
		j--;
		if (j <= 0)
			return -EIO;

159
		if ((RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_RE) == 0)
L
Linus Torvalds 已提交
160 161 162 163
			j = 0;
		else
			udelay(1);
	} while (j > 0);
164 165

	*out = RD(adap, PSC_SMBTXRX);
L
Linus Torvalds 已提交
166 167 168 169

	return 0;
}

170
static int i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
L
Linus Torvalds 已提交
171 172
		    unsigned int len)
{
173
	int i;
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182

	if (len == 0)
		return 0;

	/* A read is performed by stuffing the transmit fifo with
	 * zero bytes for timing, waiting for bytes to appear in the
	 * receive fifo, then reading the bytes.
	 */
	i = 0;
183 184 185
	while (i < (len - 1)) {
		WR(adap, PSC_SMBTXRX, 0);
		if (wait_for_rx_byte(adap, &buf[i]))
L
Linus Torvalds 已提交
186 187 188 189 190
			return -EIO;

		i++;
	}

191 192
	/* The last byte has to indicate transfer done. */
	WR(adap, PSC_SMBTXRX, PSC_SMBTXRX_STP);
L
Linus Torvalds 已提交
193 194 195
	if (wait_master_done(adap))
		return -EIO;

196
	buf[i] = (unsigned char)(RD(adap, PSC_SMBTXRX) & 0xff);
L
Linus Torvalds 已提交
197 198 199
	return 0;
}

200
static int i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
L
Linus Torvalds 已提交
201 202
		     unsigned int len)
{
203 204
	int i;
	unsigned long data;
L
Linus Torvalds 已提交
205 206 207 208 209 210 211

	if (len == 0)
		return 0;

	i = 0;
	while (i < (len-1)) {
		data = buf[i];
212
		WR(adap, PSC_SMBTXRX, data);
L
Linus Torvalds 已提交
213 214 215 216 217
		if (wait_ack(adap))
			return -EIO;
		i++;
	}

218
	/* The last byte has to indicate transfer done. */
L
Linus Torvalds 已提交
219 220
	data = buf[i];
	data |= PSC_SMBTXRX_STP;
221
	WR(adap, PSC_SMBTXRX, data);
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231 232 233
	if (wait_master_done(adap))
		return -EIO;
	return 0;
}

static int
au1550_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
{
	struct i2c_au1550_data *adap = i2c_adap->algo_data;
	struct i2c_msg *p;
	int i, err = 0;

234
	WR(adap, PSC_CTRL, PSC_CTRL_ENABLE);
M
Manuel Lauss 已提交
235

L
Linus Torvalds 已提交
236 237
	for (i = 0; !err && i < num; i++) {
		p = &msgs[i];
238 239
		err = do_address(adap, p->addr, p->flags & I2C_M_RD,
				 (p->len == 0));
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251
		if (err || !p->len)
			continue;
		if (p->flags & I2C_M_RD)
			err = i2c_read(adap, p->buf, p->len);
		else
			err = i2c_write(adap, p->buf, p->len);
	}

	/* Return the number of messages processed, or the error code.
	*/
	if (err == 0)
		err = num;
M
Manuel Lauss 已提交
252

253
	WR(adap, PSC_CTRL, PSC_CTRL_SUSPEND);
M
Manuel Lauss 已提交
254

L
Linus Torvalds 已提交
255 256 257
	return err;
}

258
static u32 au1550_func(struct i2c_adapter *adap)
L
Linus Torvalds 已提交
259
{
260
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
L
Linus Torvalds 已提交
261 262
}

263
static const struct i2c_algorithm au1550_algo = {
L
Linus Torvalds 已提交
264 265 266 267
	.master_xfer	= au1550_xfer,
	.functionality	= au1550_func,
};

M
Manuel Lauss 已提交
268 269
static void i2c_au1550_setup(struct i2c_au1550_data *priv)
{
270
	unsigned long cfg;
M
Manuel Lauss 已提交
271

272 273 274 275 276 277 278 279 280
	WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
	WR(priv, PSC_SEL, PSC_SEL_PS_SMBUSMODE);
	WR(priv, PSC_SMBCFG, 0);
	WR(priv, PSC_CTRL, PSC_CTRL_ENABLE);
	while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
		cpu_relax();

	cfg = PSC_SMBCFG_RT_FIFO8 | PSC_SMBCFG_TT_FIFO8 | PSC_SMBCFG_DD_DISABLE;
	WR(priv, PSC_SMBCFG, cfg);
M
Manuel Lauss 已提交
281 282 283 284

	/* Divide by 8 to get a 6.25 MHz clock.  The later protocol
	 * timings are based on this clock.
	 */
285 286 287
	cfg |= PSC_SMBCFG_SET_DIV(PSC_SMBCFG_DIV8);
	WR(priv, PSC_SMBCFG, cfg);
	WR(priv, PSC_SMBMSK, PSC_SMBMSK_ALLMASK);
M
Manuel Lauss 已提交
288 289 290 291

	/* Set the protocol timer values.  See Table 71 in the
	 * Au1550 Data Book for standard timing values.
	 */
292
	WR(priv, PSC_SMBTMR, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(15) | \
M
Manuel Lauss 已提交
293 294
		PSC_SMBTMR_SET_PU(15) | PSC_SMBTMR_SET_SH(15) | \
		PSC_SMBTMR_SET_SU(15) | PSC_SMBTMR_SET_CL(15) | \
295
		PSC_SMBTMR_SET_CH(15));
M
Manuel Lauss 已提交
296

297 298 299 300
	cfg |= PSC_SMBCFG_DE_ENABLE;
	WR(priv, PSC_SMBCFG, cfg);
	while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
		cpu_relax();
M
Manuel Lauss 已提交
301

302
	WR(priv, PSC_CTRL, PSC_CTRL_SUSPEND);
M
Manuel Lauss 已提交
303 304 305 306
}

static void i2c_au1550_disable(struct i2c_au1550_data *priv)
{
307 308
	WR(priv, PSC_SMBCFG, 0);
	WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
M
Manuel Lauss 已提交
309 310
}

L
Linus Torvalds 已提交
311 312 313 314 315
/*
 * registering functions to load algorithms at runtime
 * Prior to calling us, the 50MHz clock frequency and routing
 * must have been set up for the PSC indicated by the adapter.
 */
316 317
static int __devinit
i2c_au1550_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
318
{
319 320 321 322 323 324 325 326 327
	struct i2c_au1550_data *priv;
	struct resource *r;
	int ret;

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		ret = -ENODEV;
		goto out;
	}
L
Linus Torvalds 已提交
328

329 330 331 332 333 334
	priv = kzalloc(sizeof(struct i2c_au1550_data), GFP_KERNEL);
	if (!priv) {
		ret = -ENOMEM;
		goto out;
	}

L
Linus Walleij 已提交
335
	priv->ioarea = request_mem_region(r->start, resource_size(r),
336 337 338 339 340 341
					  pdev->name);
	if (!priv->ioarea) {
		ret = -EBUSY;
		goto out_mem;
	}

342 343 344 345 346
	priv->psc_base = ioremap(r->start, resource_size(r));
	if (!priv->psc_base) {
		ret = -EIO;
		goto out_map;
	}
347 348 349 350 351 352 353
	priv->xfer_timeout = 200;

	priv->adap.nr = pdev->id;
	priv->adap.algo = &au1550_algo;
	priv->adap.algo_data = priv;
	priv->adap.dev.parent = &pdev->dev;
	strlcpy(priv->adap.name, "Au1xxx PSC I2C", sizeof(priv->adap.name));
L
Linus Torvalds 已提交
354

355
	/* Now, set up the PSC for SMBus PIO mode. */
M
Manuel Lauss 已提交
356
	i2c_au1550_setup(priv);
L
Linus Torvalds 已提交
357

358 359 360 361 362 363
	ret = i2c_add_numbered_adapter(&priv->adap);
	if (ret == 0) {
		platform_set_drvdata(pdev, priv);
		return 0;
	}

M
Manuel Lauss 已提交
364
	i2c_au1550_disable(priv);
365 366
	iounmap(priv->psc_base);
out_map:
367 368 369 370 371 372 373
	release_resource(priv->ioarea);
	kfree(priv->ioarea);
out_mem:
	kfree(priv);
out:
	return ret;
}
L
Linus Torvalds 已提交
374

375
static int __devexit i2c_au1550_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
376
{
377 378 379 380
	struct i2c_au1550_data *priv = platform_get_drvdata(pdev);

	platform_set_drvdata(pdev, NULL);
	i2c_del_adapter(&priv->adap);
M
Manuel Lauss 已提交
381
	i2c_au1550_disable(priv);
382
	iounmap(priv->psc_base);
383 384 385 386
	release_resource(priv->ioarea);
	kfree(priv->ioarea);
	kfree(priv);
	return 0;
L
Linus Torvalds 已提交
387 388
}

M
Manuel Lauss 已提交
389
#ifdef CONFIG_PM
M
Manuel Lauss 已提交
390
static int i2c_au1550_suspend(struct device *dev)
L
Linus Torvalds 已提交
391
{
M
Manuel Lauss 已提交
392
	struct i2c_au1550_data *priv = dev_get_drvdata(dev);
393

M
Manuel Lauss 已提交
394 395
	i2c_au1550_disable(priv);

L
Linus Torvalds 已提交
396 397 398
	return 0;
}

M
Manuel Lauss 已提交
399
static int i2c_au1550_resume(struct device *dev)
L
Linus Torvalds 已提交
400
{
M
Manuel Lauss 已提交
401
	struct i2c_au1550_data *priv = dev_get_drvdata(dev);
402

M
Manuel Lauss 已提交
403 404
	i2c_au1550_setup(priv);

L
Linus Torvalds 已提交
405 406
	return 0;
}
M
Manuel Lauss 已提交
407 408 409 410 411 412 413 414

static const struct dev_pm_ops i2c_au1550_pmops = {
	.suspend	= i2c_au1550_suspend,
	.resume		= i2c_au1550_resume,
};

#define AU1XPSC_SMBUS_PMOPS (&i2c_au1550_pmops)

M
Manuel Lauss 已提交
415
#else
M
Manuel Lauss 已提交
416
#define AU1XPSC_SMBUS_PMOPS NULL
M
Manuel Lauss 已提交
417
#endif
L
Linus Torvalds 已提交
418

419 420 421 422
static struct platform_driver au1xpsc_smbus_driver = {
	.driver = {
		.name	= "au1xpsc_smbus",
		.owner	= THIS_MODULE,
M
Manuel Lauss 已提交
423
		.pm	= AU1XPSC_SMBUS_PMOPS,
424 425 426
	},
	.probe		= i2c_au1550_probe,
	.remove		= __devexit_p(i2c_au1550_remove),
L
Linus Torvalds 已提交
427 428
};

429
module_platform_driver(au1xpsc_smbus_driver);
L
Linus Torvalds 已提交
430 431 432 433

MODULE_AUTHOR("Dan Malek, Embedded Edge, LLC.");
MODULE_DESCRIPTION("SMBus adapter Alchemy pb1550");
MODULE_LICENSE("GPL");
434
MODULE_ALIAS("platform:au1xpsc_smbus");