i2c-au1550.c 9.4 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
/*
 * 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.
 */

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
29
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
30 31
#include <linux/errno.h>
#include <linux/i2c.h>
32
#include <linux/slab.h>
L
Linus Torvalds 已提交
33

M
Manuel Lauss 已提交
34
#include <asm/mach-au1x00/au1000.h>
L
Linus Torvalds 已提交
35 36
#include <asm/mach-au1x00/au1xxx_psc.h>

37 38 39 40 41 42 43 44 45 46
#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

47
struct i2c_au1550_data {
48
	void __iomem *psc_base;
49 50 51 52
	int	xfer_timeout;
	struct i2c_adapter adap;
	struct resource *ioarea;
};
L
Linus Torvalds 已提交
53

54
static inline void WR(struct i2c_au1550_data *a, int r, unsigned long v)
L
Linus Torvalds 已提交
55
{
56 57 58
	__raw_writel(v, a->psc_base + r);
	wmb();
}
L
Linus Torvalds 已提交
59

60 61 62 63
static inline unsigned long RD(struct i2c_au1550_data *a, int r)
{
	return __raw_readl(a->psc_base + r);
}
L
Linus Torvalds 已提交
64

65 66 67 68 69
static int wait_xfer_done(struct i2c_au1550_data *adap)
{
	int i;

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

L
Linus Torvalds 已提交
74 75 76 77 78 79
		udelay(1);
	}

	return -ETIMEDOUT;
}

80
static int wait_ack(struct i2c_au1550_data *adap)
L
Linus Torvalds 已提交
81
{
82
	unsigned long stat;
L
Linus Torvalds 已提交
83 84 85 86

	if (wait_xfer_done(adap))
		return -ETIMEDOUT;

87
	stat = RD(adap, PSC_SMBEVNT);
L
Linus Torvalds 已提交
88 89 90 91 92 93
	if ((stat & (PSC_SMBEVNT_DN | PSC_SMBEVNT_AN | PSC_SMBEVNT_AL)) != 0)
		return -ETIMEDOUT;

	return 0;
}

94
static int wait_master_done(struct i2c_au1550_data *adap)
L
Linus Torvalds 已提交
95
{
96
	int i;
L
Linus Torvalds 已提交
97

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

	return -ETIMEDOUT;
}

static int
109
do_address(struct i2c_au1550_data *adap, unsigned int addr, int rd, int q)
L
Linus Torvalds 已提交
110
{
111
	unsigned long stat;
L
Linus Torvalds 已提交
112

113 114 115
	/* Reset the FIFOs, clear events. */
	stat = RD(adap, PSC_SMBSTAT);
	WR(adap, PSC_SMBEVNT, PSC_SMBEVNT_ALLCLR);
D
Domen Puncer 已提交
116 117

	if (!(stat & PSC_SMBSTAT_TE) || !(stat & PSC_SMBSTAT_RE)) {
118 119 120
		WR(adap, PSC_SMBPCR, PSC_SMBPCR_DC);
		while ((RD(adap, PSC_SMBPCR) & PSC_SMBPCR_DC) != 0)
			cpu_relax();
D
Domen Puncer 已提交
121 122
		udelay(50);
	}
L
Linus Torvalds 已提交
123

124
	/* Write out the i2c chip address and specify operation */
L
Linus Torvalds 已提交
125 126 127 128
	addr <<= 1;
	if (rd)
		addr |= 1;

129 130 131 132
	/* zero-byte xfers stop immediately */
	if (q)
		addr |= PSC_SMBTXRX_STP;

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

141
static int wait_for_rx_byte(struct i2c_au1550_data *adap, unsigned char *out)
L
Linus Torvalds 已提交
142
{
143
	int j;
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153

	if (wait_xfer_done(adap))
		return -EIO;

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

154
		if ((RD(adap, PSC_SMBSTAT) & PSC_SMBSTAT_RE) == 0)
L
Linus Torvalds 已提交
155 156 157 158
			j = 0;
		else
			udelay(1);
	} while (j > 0);
159 160

	*out = RD(adap, PSC_SMBTXRX);
L
Linus Torvalds 已提交
161 162 163 164

	return 0;
}

165
static int i2c_read(struct i2c_au1550_data *adap, unsigned char *buf,
L
Linus Torvalds 已提交
166 167
		    unsigned int len)
{
168
	int i;
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177

	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;
178 179 180
	while (i < (len - 1)) {
		WR(adap, PSC_SMBTXRX, 0);
		if (wait_for_rx_byte(adap, &buf[i]))
L
Linus Torvalds 已提交
181 182 183 184 185
			return -EIO;

		i++;
	}

186 187
	/* The last byte has to indicate transfer done. */
	WR(adap, PSC_SMBTXRX, PSC_SMBTXRX_STP);
L
Linus Torvalds 已提交
188 189 190
	if (wait_master_done(adap))
		return -EIO;

191
	buf[i] = (unsigned char)(RD(adap, PSC_SMBTXRX) & 0xff);
L
Linus Torvalds 已提交
192 193 194
	return 0;
}

195
static int i2c_write(struct i2c_au1550_data *adap, unsigned char *buf,
L
Linus Torvalds 已提交
196 197
		     unsigned int len)
{
198 199
	int i;
	unsigned long data;
L
Linus Torvalds 已提交
200 201 202 203 204 205 206

	if (len == 0)
		return 0;

	i = 0;
	while (i < (len-1)) {
		data = buf[i];
207
		WR(adap, PSC_SMBTXRX, data);
L
Linus Torvalds 已提交
208 209 210 211 212
		if (wait_ack(adap))
			return -EIO;
		i++;
	}

213
	/* The last byte has to indicate transfer done. */
L
Linus Torvalds 已提交
214 215
	data = buf[i];
	data |= PSC_SMBTXRX_STP;
216
	WR(adap, PSC_SMBTXRX, data);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226 227 228
	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;

229
	WR(adap, PSC_CTRL, PSC_CTRL_ENABLE);
M
Manuel Lauss 已提交
230

L
Linus Torvalds 已提交
231 232
	for (i = 0; !err && i < num; i++) {
		p = &msgs[i];
233 234
		err = do_address(adap, p->addr, p->flags & I2C_M_RD,
				 (p->len == 0));
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246
		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 已提交
247

248
	WR(adap, PSC_CTRL, PSC_CTRL_SUSPEND);
M
Manuel Lauss 已提交
249

L
Linus Torvalds 已提交
250 251 252
	return err;
}

253
static u32 au1550_func(struct i2c_adapter *adap)
L
Linus Torvalds 已提交
254
{
255
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
L
Linus Torvalds 已提交
256 257
}

258
static const struct i2c_algorithm au1550_algo = {
L
Linus Torvalds 已提交
259 260 261 262
	.master_xfer	= au1550_xfer,
	.functionality	= au1550_func,
};

M
Manuel Lauss 已提交
263 264
static void i2c_au1550_setup(struct i2c_au1550_data *priv)
{
265
	unsigned long cfg;
M
Manuel Lauss 已提交
266

267 268 269 270 271 272 273 274 275
	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 已提交
276 277 278 279

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

	/* Set the protocol timer values.  See Table 71 in the
	 * Au1550 Data Book for standard timing values.
	 */
287 288 289 290
	WR(priv, PSC_SMBTMR, PSC_SMBTMR_SET_TH(0) | PSC_SMBTMR_SET_PS(20) | \
		PSC_SMBTMR_SET_PU(20) | PSC_SMBTMR_SET_SH(20) | \
		PSC_SMBTMR_SET_SU(20) | PSC_SMBTMR_SET_CL(20) | \
		PSC_SMBTMR_SET_CH(20));
M
Manuel Lauss 已提交
291

292 293 294 295
	cfg |= PSC_SMBCFG_DE_ENABLE;
	WR(priv, PSC_SMBCFG, cfg);
	while ((RD(priv, PSC_SMBSTAT) & PSC_SMBSTAT_SR) == 0)
		cpu_relax();
M
Manuel Lauss 已提交
296

297
	WR(priv, PSC_CTRL, PSC_CTRL_SUSPEND);
M
Manuel Lauss 已提交
298 299 300 301
}

static void i2c_au1550_disable(struct i2c_au1550_data *priv)
{
302 303
	WR(priv, PSC_SMBCFG, 0);
	WR(priv, PSC_CTRL, PSC_CTRL_DISABLE);
M
Manuel Lauss 已提交
304 305
}

L
Linus Torvalds 已提交
306 307 308 309 310
/*
 * 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.
 */
311
static int
312
i2c_au1550_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
313
{
314 315 316 317 318 319 320 321 322
	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 已提交
323

324 325 326 327 328 329
	priv = kzalloc(sizeof(struct i2c_au1550_data), GFP_KERNEL);
	if (!priv) {
		ret = -ENOMEM;
		goto out;
	}

L
Linus Walleij 已提交
330
	priv->ioarea = request_mem_region(r->start, resource_size(r),
331 332 333 334 335 336
					  pdev->name);
	if (!priv->ioarea) {
		ret = -EBUSY;
		goto out_mem;
	}

337 338 339 340 341
	priv->psc_base = ioremap(r->start, resource_size(r));
	if (!priv->psc_base) {
		ret = -EIO;
		goto out_map;
	}
342 343 344 345 346 347 348
	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 已提交
349

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

353 354 355 356 357 358
	ret = i2c_add_numbered_adapter(&priv->adap);
	if (ret == 0) {
		platform_set_drvdata(pdev, priv);
		return 0;
	}

M
Manuel Lauss 已提交
359
	i2c_au1550_disable(priv);
360 361
	iounmap(priv->psc_base);
out_map:
362 363 364 365 366 367 368
	release_resource(priv->ioarea);
	kfree(priv->ioarea);
out_mem:
	kfree(priv);
out:
	return ret;
}
L
Linus Torvalds 已提交
369

370
static int i2c_au1550_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
371
{
372 373 374
	struct i2c_au1550_data *priv = platform_get_drvdata(pdev);

	i2c_del_adapter(&priv->adap);
M
Manuel Lauss 已提交
375
	i2c_au1550_disable(priv);
376
	iounmap(priv->psc_base);
377 378 379 380
	release_resource(priv->ioarea);
	kfree(priv->ioarea);
	kfree(priv);
	return 0;
L
Linus Torvalds 已提交
381 382
}

M
Manuel Lauss 已提交
383
#ifdef CONFIG_PM
M
Manuel Lauss 已提交
384
static int i2c_au1550_suspend(struct device *dev)
L
Linus Torvalds 已提交
385
{
M
Manuel Lauss 已提交
386
	struct i2c_au1550_data *priv = dev_get_drvdata(dev);
387

M
Manuel Lauss 已提交
388 389
	i2c_au1550_disable(priv);

L
Linus Torvalds 已提交
390 391 392
	return 0;
}

M
Manuel Lauss 已提交
393
static int i2c_au1550_resume(struct device *dev)
L
Linus Torvalds 已提交
394
{
M
Manuel Lauss 已提交
395
	struct i2c_au1550_data *priv = dev_get_drvdata(dev);
396

M
Manuel Lauss 已提交
397 398
	i2c_au1550_setup(priv);

L
Linus Torvalds 已提交
399 400
	return 0;
}
M
Manuel Lauss 已提交
401 402 403 404 405 406 407 408

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 已提交
409
#else
M
Manuel Lauss 已提交
410
#define AU1XPSC_SMBUS_PMOPS NULL
M
Manuel Lauss 已提交
411
#endif
L
Linus Torvalds 已提交
412

413 414 415
static struct platform_driver au1xpsc_smbus_driver = {
	.driver = {
		.name	= "au1xpsc_smbus",
M
Manuel Lauss 已提交
416
		.pm	= AU1XPSC_SMBUS_PMOPS,
417 418
	},
	.probe		= i2c_au1550_probe,
419
	.remove		= i2c_au1550_remove,
L
Linus Torvalds 已提交
420 421
};

422
module_platform_driver(au1xpsc_smbus_driver);
L
Linus Torvalds 已提交
423 424 425 426

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