sa1111.c 36.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * linux/arch/arm/common/sa1111.c
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * SA1111 support
 *
 * Original code by John Dorsey
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This file contains all generic SA1111 support.
 *
 * All initialization functions provided here are intended to be called
 * from machine specific code with proper arguments when required.
 */
#include <linux/module.h>
#include <linux/init.h>
19
#include <linux/irq.h>
L
Linus Torvalds 已提交
20 21 22 23
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/ioport.h>
24
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
25 26 27
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/dma-mapping.h>
28
#include <linux/clk.h>
29
#include <linux/io.h>
L
Linus Torvalds 已提交
30

31
#include <mach/hardware.h>
L
Linus Torvalds 已提交
32
#include <asm/mach/irq.h>
33
#include <asm/mach-types.h>
R
Russell King 已提交
34
#include <asm/sizes.h>
L
Linus Torvalds 已提交
35 36 37

#include <asm/hardware/sa1111.h>

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/* SA1111 IRQs */
#define IRQ_GPAIN0		(0)
#define IRQ_GPAIN1		(1)
#define IRQ_GPAIN2		(2)
#define IRQ_GPAIN3		(3)
#define IRQ_GPBIN0		(4)
#define IRQ_GPBIN1		(5)
#define IRQ_GPBIN2		(6)
#define IRQ_GPBIN3		(7)
#define IRQ_GPBIN4		(8)
#define IRQ_GPBIN5		(9)
#define IRQ_GPCIN0		(10)
#define IRQ_GPCIN1		(11)
#define IRQ_GPCIN2		(12)
#define IRQ_GPCIN3		(13)
#define IRQ_GPCIN4		(14)
#define IRQ_GPCIN5		(15)
#define IRQ_GPCIN6		(16)
#define IRQ_GPCIN7		(17)
#define IRQ_MSTXINT		(18)
#define IRQ_MSRXINT		(19)
#define IRQ_MSSTOPERRINT	(20)
#define IRQ_TPTXINT		(21)
#define IRQ_TPRXINT		(22)
#define IRQ_TPSTOPERRINT	(23)
#define SSPXMTINT		(24)
#define SSPRCVINT		(25)
#define SSPROR			(26)
#define AUDXMTDMADONEA		(32)
#define AUDRCVDMADONEA		(33)
#define AUDXMTDMADONEB		(34)
#define AUDRCVDMADONEB		(35)
#define AUDTFSR			(36)
#define AUDRFSR			(37)
#define AUDTUR			(38)
#define AUDROR			(39)
#define AUDDTS			(40)
#define AUDRDD			(41)
#define AUDSTO			(42)
#define IRQ_USBPWR		(43)
#define IRQ_HCIM		(44)
#define IRQ_HCIBUFFACC		(45)
#define IRQ_HCIRMTWKP		(46)
#define IRQ_NHCIMFCIR		(47)
#define IRQ_USB_PORT_RESUME	(48)
#define IRQ_S0_READY_NINT	(49)
#define IRQ_S1_READY_NINT	(50)
#define IRQ_S0_CD_VALID		(51)
#define IRQ_S1_CD_VALID		(52)
#define IRQ_S0_BVD1_STSCHG	(53)
#define IRQ_S1_BVD1_STSCHG	(54)
89
#define SA1111_IRQ_NR		(55)
90

91 92
extern void sa1110_mb_enable(void);
extern void sa1110_mb_disable(void);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101 102

/*
 * We keep the following data for the overall SA1111.  Note that the
 * struct device and struct resource are "fake"; they should be supplied
 * by the bus above us.  However, in the interests of getting all SA1111
 * drivers converted over to the device model, we provide this as an
 * anchor point for all the other drivers.
 */
struct sa1111 {
	struct device	*dev;
103
	struct clk	*clk;
L
Linus Torvalds 已提交
104 105
	unsigned long	phys;
	int		irq;
106
	int		irq_base;	/* base for cascaded on-chip IRQs */
L
Linus Torvalds 已提交
107 108
	spinlock_t	lock;
	void __iomem	*base;
109
	struct sa1111_platform_data *pdata;
110 111 112
#ifdef CONFIG_PM
	void		*saved_state;
#endif
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
};

/*
 * We _really_ need to eliminate this.  Its only users
 * are the PWM and DMA checking code.
 */
static struct sa1111 *g_sa1111;

struct sa1111_dev_info {
	unsigned long	offset;
	unsigned long	skpcr_mask;
	unsigned int	devid;
	unsigned int	irq[6];
};

static struct sa1111_dev_info sa1111_devices[] = {
	{
		.offset		= SA1111_USB,
		.skpcr_mask	= SKPCR_UCLKEN,
		.devid		= SA1111_DEVID_USB,
		.irq = {
			IRQ_USBPWR,
			IRQ_HCIM,
			IRQ_HCIBUFFACC,
			IRQ_HCIRMTWKP,
			IRQ_NHCIMFCIR,
			IRQ_USB_PORT_RESUME
		},
	},
	{
		.offset		= 0x0600,
		.skpcr_mask	= SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
		.devid		= SA1111_DEVID_SAC,
		.irq = {
			AUDXMTDMADONEA,
			AUDXMTDMADONEB,
			AUDRCVDMADONEA,
			AUDRCVDMADONEB
		},
	},
	{
		.offset		= 0x0800,
		.skpcr_mask	= SKPCR_SCLKEN,
		.devid		= SA1111_DEVID_SSP,
	},
	{
		.offset		= SA1111_KBD,
		.skpcr_mask	= SKPCR_PTCLKEN,
161
		.devid		= SA1111_DEVID_PS2_KBD,
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169
		.irq = {
			IRQ_TPRXINT,
			IRQ_TPTXINT
		},
	},
	{
		.offset		= SA1111_MSE,
		.skpcr_mask	= SKPCR_PMCLKEN,
170
		.devid		= SA1111_DEVID_PS2_MSE,
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		.irq = {
			IRQ_MSRXINT,
			IRQ_MSTXINT
		},
	},
	{
		.offset		= 0x1800,
		.skpcr_mask	= 0,
		.devid		= SA1111_DEVID_PCMCIA,
		.irq = {
			IRQ_S0_READY_NINT,
			IRQ_S0_CD_VALID,
			IRQ_S0_BVD1_STSCHG,
			IRQ_S1_READY_NINT,
			IRQ_S1_CD_VALID,
			IRQ_S1_BVD1_STSCHG,
		},
	},
};

/*
 * SA1111 interrupt support.  Since clearing an IRQ while there are
 * active IRQs causes the interrupt output to pulse, the upper levels
 * will call us again if there are more interrupts to process.
 */
static void
197
sa1111_irq_handler(unsigned int irq, struct irq_desc *desc)
L
Linus Torvalds 已提交
198 199
{
	unsigned int stat0, stat1, i;
T
Thomas Gleixner 已提交
200
	struct sa1111 *sachip = irq_get_handler_data(irq);
201
	void __iomem *mapbase = sachip->base + SA1111_INTC;
L
Linus Torvalds 已提交
202

203 204
	stat0 = sa1111_readl(mapbase + SA1111_INTSTATCLR0);
	stat1 = sa1111_readl(mapbase + SA1111_INTSTATCLR1);
L
Linus Torvalds 已提交
205

206
	sa1111_writel(stat0, mapbase + SA1111_INTSTATCLR0);
L
Linus Torvalds 已提交
207

208
	desc->irq_data.chip->irq_ack(&desc->irq_data);
L
Linus Torvalds 已提交
209

210
	sa1111_writel(stat1, mapbase + SA1111_INTSTATCLR1);
L
Linus Torvalds 已提交
211 212

	if (stat0 == 0 && stat1 == 0) {
213
		do_bad_IRQ(irq, desc);
L
Linus Torvalds 已提交
214 215 216
		return;
	}

217
	for (i = 0; stat0; i++, stat0 >>= 1)
L
Linus Torvalds 已提交
218
		if (stat0 & 1)
219
			generic_handle_irq(i + sachip->irq_base);
L
Linus Torvalds 已提交
220

221
	for (i = 32; stat1; i++, stat1 >>= 1)
L
Linus Torvalds 已提交
222
		if (stat1 & 1)
223
			generic_handle_irq(i + sachip->irq_base);
L
Linus Torvalds 已提交
224 225

	/* For level-based interrupts */
226
	desc->irq_data.chip->irq_unmask(&desc->irq_data);
L
Linus Torvalds 已提交
227 228
}

229 230
#define SA1111_IRQMASK_LO(x)	(1 << (x - sachip->irq_base))
#define SA1111_IRQMASK_HI(x)	(1 << (x - sachip->irq_base - 32))
L
Linus Torvalds 已提交
231

232
static void sa1111_ack_irq(struct irq_data *d)
L
Linus Torvalds 已提交
233 234 235
{
}

236
static void sa1111_mask_lowirq(struct irq_data *d)
L
Linus Torvalds 已提交
237
{
238
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
239
	void __iomem *mapbase = sachip->base + SA1111_INTC;
L
Linus Torvalds 已提交
240 241 242
	unsigned long ie0;

	ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
243
	ie0 &= ~SA1111_IRQMASK_LO(d->irq);
L
Linus Torvalds 已提交
244 245 246
	writel(ie0, mapbase + SA1111_INTEN0);
}

247
static void sa1111_unmask_lowirq(struct irq_data *d)
L
Linus Torvalds 已提交
248
{
249
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
250
	void __iomem *mapbase = sachip->base + SA1111_INTC;
L
Linus Torvalds 已提交
251 252 253
	unsigned long ie0;

	ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
254
	ie0 |= SA1111_IRQMASK_LO(d->irq);
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262 263 264
	sa1111_writel(ie0, mapbase + SA1111_INTEN0);
}

/*
 * Attempt to re-trigger the interrupt.  The SA1111 contains a register
 * (INTSET) which claims to do this.  However, in practice no amount of
 * manipulation of INTEN and INTSET guarantees that the interrupt will
 * be triggered.  In fact, its very difficult, if not impossible to get
 * INTSET to re-trigger the interrupt.
 */
265
static int sa1111_retrigger_lowirq(struct irq_data *d)
L
Linus Torvalds 已提交
266
{
267
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
268
	void __iomem *mapbase = sachip->base + SA1111_INTC;
269
	unsigned int mask = SA1111_IRQMASK_LO(d->irq);
L
Linus Torvalds 已提交
270 271 272 273 274 275 276
	unsigned long ip0;
	int i;

	ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
	for (i = 0; i < 8; i++) {
		sa1111_writel(ip0 ^ mask, mapbase + SA1111_INTPOL0);
		sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
277
		if (sa1111_readl(mapbase + SA1111_INTSTATCLR0) & mask)
L
Linus Torvalds 已提交
278 279 280 281 282
			break;
	}

	if (i == 8)
		printk(KERN_ERR "Danger Will Robinson: failed to "
283
			"re-trigger IRQ%d\n", d->irq);
L
Linus Torvalds 已提交
284 285 286
	return i == 8 ? -1 : 0;
}

287
static int sa1111_type_lowirq(struct irq_data *d, unsigned int flags)
L
Linus Torvalds 已提交
288
{
289
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
290
	void __iomem *mapbase = sachip->base + SA1111_INTC;
291
	unsigned int mask = SA1111_IRQMASK_LO(d->irq);
L
Linus Torvalds 已提交
292 293
	unsigned long ip0;

294
	if (flags == IRQ_TYPE_PROBE)
L
Linus Torvalds 已提交
295 296
		return 0;

297
	if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
L
Linus Torvalds 已提交
298 299 300
		return -EINVAL;

	ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
301
	if (flags & IRQ_TYPE_EDGE_RISING)
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310
		ip0 &= ~mask;
	else
		ip0 |= mask;
	sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
	sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);

	return 0;
}

311
static int sa1111_wake_lowirq(struct irq_data *d, unsigned int on)
L
Linus Torvalds 已提交
312
{
313
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
314
	void __iomem *mapbase = sachip->base + SA1111_INTC;
315
	unsigned int mask = SA1111_IRQMASK_LO(d->irq);
L
Linus Torvalds 已提交
316 317 318 319 320 321 322 323 324 325 326 327
	unsigned long we0;

	we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
	if (on)
		we0 |= mask;
	else
		we0 &= ~mask;
	sa1111_writel(we0, mapbase + SA1111_WAKEEN0);

	return 0;
}

328 329
static struct irq_chip sa1111_low_chip = {
	.name		= "SA1111-l",
330 331 332 333 334 335
	.irq_ack	= sa1111_ack_irq,
	.irq_mask	= sa1111_mask_lowirq,
	.irq_unmask	= sa1111_unmask_lowirq,
	.irq_retrigger	= sa1111_retrigger_lowirq,
	.irq_set_type	= sa1111_type_lowirq,
	.irq_set_wake	= sa1111_wake_lowirq,
L
Linus Torvalds 已提交
336 337
};

338
static void sa1111_mask_highirq(struct irq_data *d)
L
Linus Torvalds 已提交
339
{
340
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
341
	void __iomem *mapbase = sachip->base + SA1111_INTC;
L
Linus Torvalds 已提交
342 343 344
	unsigned long ie1;

	ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
345
	ie1 &= ~SA1111_IRQMASK_HI(d->irq);
L
Linus Torvalds 已提交
346 347 348
	sa1111_writel(ie1, mapbase + SA1111_INTEN1);
}

349
static void sa1111_unmask_highirq(struct irq_data *d)
L
Linus Torvalds 已提交
350
{
351
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
352
	void __iomem *mapbase = sachip->base + SA1111_INTC;
L
Linus Torvalds 已提交
353 354 355
	unsigned long ie1;

	ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
356
	ie1 |= SA1111_IRQMASK_HI(d->irq);
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366
	sa1111_writel(ie1, mapbase + SA1111_INTEN1);
}

/*
 * Attempt to re-trigger the interrupt.  The SA1111 contains a register
 * (INTSET) which claims to do this.  However, in practice no amount of
 * manipulation of INTEN and INTSET guarantees that the interrupt will
 * be triggered.  In fact, its very difficult, if not impossible to get
 * INTSET to re-trigger the interrupt.
 */
367
static int sa1111_retrigger_highirq(struct irq_data *d)
L
Linus Torvalds 已提交
368
{
369
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
370
	void __iomem *mapbase = sachip->base + SA1111_INTC;
371
	unsigned int mask = SA1111_IRQMASK_HI(d->irq);
L
Linus Torvalds 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384
	unsigned long ip1;
	int i;

	ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
	for (i = 0; i < 8; i++) {
		sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
		sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
		if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
			break;
	}

	if (i == 8)
		printk(KERN_ERR "Danger Will Robinson: failed to "
385
			"re-trigger IRQ%d\n", d->irq);
L
Linus Torvalds 已提交
386 387 388
	return i == 8 ? -1 : 0;
}

389
static int sa1111_type_highirq(struct irq_data *d, unsigned int flags)
L
Linus Torvalds 已提交
390
{
391
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
392
	void __iomem *mapbase = sachip->base + SA1111_INTC;
393
	unsigned int mask = SA1111_IRQMASK_HI(d->irq);
L
Linus Torvalds 已提交
394 395
	unsigned long ip1;

396
	if (flags == IRQ_TYPE_PROBE)
L
Linus Torvalds 已提交
397 398
		return 0;

399
	if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
L
Linus Torvalds 已提交
400 401 402
		return -EINVAL;

	ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
403
	if (flags & IRQ_TYPE_EDGE_RISING)
L
Linus Torvalds 已提交
404 405 406 407 408 409 410 411 412
		ip1 &= ~mask;
	else
		ip1 |= mask;
	sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
	sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);

	return 0;
}

413
static int sa1111_wake_highirq(struct irq_data *d, unsigned int on)
L
Linus Torvalds 已提交
414
{
415
	struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
416
	void __iomem *mapbase = sachip->base + SA1111_INTC;
417
	unsigned int mask = SA1111_IRQMASK_HI(d->irq);
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429
	unsigned long we1;

	we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
	if (on)
		we1 |= mask;
	else
		we1 &= ~mask;
	sa1111_writel(we1, mapbase + SA1111_WAKEEN1);

	return 0;
}

430 431
static struct irq_chip sa1111_high_chip = {
	.name		= "SA1111-h",
432 433 434 435 436 437
	.irq_ack	= sa1111_ack_irq,
	.irq_mask	= sa1111_mask_highirq,
	.irq_unmask	= sa1111_unmask_highirq,
	.irq_retrigger	= sa1111_retrigger_highirq,
	.irq_set_type	= sa1111_type_highirq,
	.irq_set_wake	= sa1111_wake_highirq,
L
Linus Torvalds 已提交
438 439
};

440
static int sa1111_setup_irq(struct sa1111 *sachip, unsigned irq_base)
L
Linus Torvalds 已提交
441 442
{
	void __iomem *irqbase = sachip->base + SA1111_INTC;
443
	unsigned i, irq;
444
	int ret;
L
Linus Torvalds 已提交
445 446 447 448 449 450

	/*
	 * We're guaranteed that this region hasn't been taken.
	 */
	request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");

451 452 453 454 455 456 457 458 459 460 461
	ret = irq_alloc_descs(-1, irq_base, SA1111_IRQ_NR, -1);
	if (ret <= 0) {
		dev_err(sachip->dev, "unable to allocate %u irqs: %d\n",
			SA1111_IRQ_NR, ret);
		if (ret == 0)
			ret = -EINVAL;
		return ret;
	}

	sachip->irq_base = ret;

L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	/* disable all IRQs */
	sa1111_writel(0, irqbase + SA1111_INTEN0);
	sa1111_writel(0, irqbase + SA1111_INTEN1);
	sa1111_writel(0, irqbase + SA1111_WAKEEN0);
	sa1111_writel(0, irqbase + SA1111_WAKEEN1);

	/*
	 * detect on rising edge.  Note: Feb 2001 Errata for SA1111
	 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
	 */
	sa1111_writel(0, irqbase + SA1111_INTPOL0);
	sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
		      SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
		      irqbase + SA1111_INTPOL1);

	/* clear all IRQs */
	sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
	sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);

481 482
	for (i = IRQ_GPAIN0; i <= SSPROR; i++) {
		irq = sachip->irq_base + i;
483 484
		irq_set_chip_and_handler(irq, &sa1111_low_chip,
					 handle_edge_irq);
485
		irq_set_chip_data(irq, sachip);
L
Linus Torvalds 已提交
486 487 488
		set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
	}

489 490
	for (i = AUDXMTDMADONEA; i <= IRQ_S1_BVD1_STSCHG; i++) {
		irq = sachip->irq_base + i;
491 492
		irq_set_chip_and_handler(irq, &sa1111_high_chip,
					 handle_edge_irq);
493
		irq_set_chip_data(irq, sachip);
L
Linus Torvalds 已提交
494 495 496 497 498 499
		set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
	}

	/*
	 * Register SA1111 interrupt
	 */
T
Thomas Gleixner 已提交
500 501 502
	irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
	irq_set_handler_data(sachip->irq, sachip);
	irq_set_chained_handler(sachip->irq, sa1111_irq_handler);
503 504 505 506 507

	dev_info(sachip->dev, "Providing IRQ%u-%u\n",
		sachip->irq_base, sachip->irq_base + SA1111_IRQ_NR - 1);

	return 0;
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
}

/*
 * Bring the SA1111 out of reset.  This requires a set procedure:
 *  1. nRESET asserted (by hardware)
 *  2. CLK turned on from SA1110
 *  3. nRESET deasserted
 *  4. VCO turned on, PLL_BYPASS turned off
 *  5. Wait lock time, then assert RCLKEn
 *  7. PCR set to allow clocking of individual functions
 *
 * Until we've done this, the only registers we can access are:
 *   SBI_SKCR
 *   SBI_SMCR
 *   SBI_SKID
 */
static void sa1111_wake(struct sa1111 *sachip)
{
	unsigned long flags, r;

	spin_lock_irqsave(&sachip->lock, flags);

530
	clk_enable(sachip->clk);
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

	/*
	 * Turn VCO on, and disable PLL Bypass.
	 */
	r = sa1111_readl(sachip->base + SA1111_SKCR);
	r &= ~SKCR_VCO_OFF;
	sa1111_writel(r, sachip->base + SA1111_SKCR);
	r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
	sa1111_writel(r, sachip->base + SA1111_SKCR);

	/*
	 * Wait lock time.  SA1111 manual _doesn't_
	 * specify a figure for this!  We choose 100us.
	 */
	udelay(100);

	/*
	 * Enable RCLK.  We also ensure that RDYEN is set.
	 */
	r |= SKCR_RCLKEN | SKCR_RDYEN;
	sa1111_writel(r, sachip->base + SA1111_SKCR);

	/*
	 * Wait 14 RCLK cycles for the chip to finish coming out
	 * of reset. (RCLK=24MHz).  This is 590ns.
	 */
	udelay(1);

	/*
	 * Ensure all clocks are initially off.
	 */
	sa1111_writel(0, sachip->base + SA1111_SKPCR);

	spin_unlock_irqrestore(&sachip->lock, flags);
}

#ifdef CONFIG_ARCH_SA1100

static u32 sa1111_dma_mask[] = {
	~0,
	~(1 << 20),
	~(1 << 23),
	~(1 << 24),
	~(1 << 25),
	~(1 << 20),
	~(1 << 20),
	0,
};

/*
 * Configure the SA1111 shared memory controller.
 */
void
sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
		     unsigned int cas_latency)
{
	unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);

	if (cas_latency == 3)
		smcr |= SMCR_CLAT;

	sa1111_writel(smcr, sachip->base + SA1111_SMCR);

	/*
	 * Now clear the bits in the DMA mask to work around the SA1111
	 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
	 * Chip Specification Update, June 2000, Erratum #7).
	 */
	if (sachip->dev->dma_mask)
		*sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];

	sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
}
604
#endif
L
Linus Torvalds 已提交
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
#ifdef CONFIG_DMABOUNCE
/*
 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
 * Chip Specification Update" (June 2000), erratum #7, there is a
 * significant bug in the SA1111 SDRAM shared memory controller.  If
 * an access to a region of memory above 1MB relative to the bank base,
 * it is important that address bit 10 _NOT_ be asserted. Depending
 * on the configuration of the RAM, bit 10 may correspond to one
 * of several different (processor-relative) address bits.
 *
 * This routine only identifies whether or not a given DMA address
 * is susceptible to the bug.
 *
 * This should only get called for sa1111_device types due to the
 * way we configure our device dma_masks.
 */
static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
{
	/*
	 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
	 * User's Guide" mentions that jumpers R51 and R52 control the
	 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
	 * SDRAM bank 1 on Neponset). The default configuration selects
	 * Assabet, so any address in bank 1 is necessarily invalid.
	 */
	return (machine_is_assabet() || machine_is_pfs168()) &&
		(addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
}
L
Linus Torvalds 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
#endif

static void sa1111_dev_release(struct device *_dev)
{
	struct sa1111_dev *dev = SA1111_DEV(_dev);

	release_resource(&dev->res);
	kfree(dev);
}

static int
sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
		      struct sa1111_dev_info *info)
{
	struct sa1111_dev *dev;
649
	unsigned i;
L
Linus Torvalds 已提交
650 651
	int ret;

652
	dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
653 654 655 656 657
	if (!dev) {
		ret = -ENOMEM;
		goto out;
	}

658
	dev_set_name(&dev->dev, "%4.4lx", info->offset);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665
	dev->devid	 = info->devid;
	dev->dev.parent  = sachip->dev;
	dev->dev.bus     = &sa1111_bus_type;
	dev->dev.release = sa1111_dev_release;
	dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
	dev->res.start   = sachip->phys + info->offset;
	dev->res.end     = dev->res.start + 511;
666
	dev->res.name    = dev_name(&dev->dev);
L
Linus Torvalds 已提交
667 668 669
	dev->res.flags   = IORESOURCE_MEM;
	dev->mapbase     = sachip->base + info->offset;
	dev->skpcr_mask  = info->skpcr_mask;
670 671 672

	for (i = 0; i < ARRAY_SIZE(info->irq); i++)
		dev->irq[i] = sachip->irq_base + info->irq[i];
L
Linus Torvalds 已提交
673 674 675 676 677

	ret = request_resource(parent, &dev->res);
	if (ret) {
		printk("SA1111: failed to allocate resource for %s\n",
			dev->res.name);
678
		dev_set_name(&dev->dev, NULL);
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687 688 689 690
		kfree(dev);
		goto out;
	}


	ret = device_register(&dev->dev);
	if (ret) {
		release_resource(&dev->res);
		kfree(dev);
		goto out;
	}

691
#ifdef CONFIG_DMABOUNCE
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700
	/*
	 * If the parent device has a DMA mask associated with it,
	 * propagate it down to the children.
	 */
	if (sachip->dev->dma_mask) {
		dev->dma_mask = *sachip->dev->dma_mask;
		dev->dev.dma_mask = &dev->dma_mask;

		if (dev->dma_mask != 0xffffffffUL) {
701 702
			ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
					sa1111_needs_bounce);
L
Linus Torvalds 已提交
703
			if (ret) {
704 705
				dev_err(&dev->dev, "SA1111: Failed to register"
					" with dmabounce\n");
L
Linus Torvalds 已提交
706 707 708 709
				device_unregister(&dev->dev);
			}
		}
	}
710
#endif
L
Linus Torvalds 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

out:
	return ret;
}

/**
 *	sa1111_probe - probe for a single SA1111 chip.
 *	@phys_addr: physical address of device.
 *
 *	Probe for a SA1111 chip.  This must be called
 *	before any other SA1111-specific code.
 *
 *	Returns:
 *	%-ENODEV	device not found.
 *	%-EBUSY		physical address already marked in-use.
726
 *	%-EINVAL	no platform data passed
L
Linus Torvalds 已提交
727 728
 *	%0		successful.
 */
729
static int __devinit
L
Linus Torvalds 已提交
730 731
__sa1111_probe(struct device *me, struct resource *mem, int irq)
{
732
	struct sa1111_platform_data *pd = me->platform_data;
L
Linus Torvalds 已提交
733 734
	struct sa1111 *sachip;
	unsigned long id;
735
	unsigned int has_devs;
L
Linus Torvalds 已提交
736 737
	int i, ret = -ENODEV;

738 739 740
	if (!pd)
		return -EINVAL;

741
	sachip = kzalloc(sizeof(struct sa1111), GFP_KERNEL);
L
Linus Torvalds 已提交
742 743 744
	if (!sachip)
		return -ENOMEM;

745
	sachip->clk = clk_get(me, "SA1111_CLK");
746
	if (IS_ERR(sachip->clk)) {
747 748 749 750
		ret = PTR_ERR(sachip->clk);
		goto err_free;
	}

751 752 753 754
	ret = clk_prepare(sachip->clk);
	if (ret)
		goto err_clkput;

L
Linus Torvalds 已提交
755 756 757 758 759
	spin_lock_init(&sachip->lock);

	sachip->dev = me;
	dev_set_drvdata(sachip->dev, sachip);

760
	sachip->pdata = pd;
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770
	sachip->phys = mem->start;
	sachip->irq = irq;

	/*
	 * Map the whole region.  This also maps the
	 * registers for our children.
	 */
	sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
	if (!sachip->base) {
		ret = -ENOMEM;
771
		goto err_clk_unprep;
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780
	}

	/*
	 * Probe for the chip.  Only touch the SBI registers.
	 */
	id = sa1111_readl(sachip->base + SA1111_SKID);
	if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
		printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
		ret = -ENODEV;
781
		goto err_unmap;
L
Linus Torvalds 已提交
782 783 784 785 786 787 788 789 790 791 792
	}

	printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
		"silicon revision %lx, metal revision %lx\n",
		(id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));

	/*
	 * We found it.  Wake the chip up, and initialise.
	 */
	sa1111_wake(sachip);

793 794 795 796 797 798 799 800 801 802
	/*
	 * The interrupt controller must be initialised before any
	 * other device to ensure that the interrupts are available.
	 */
	if (sachip->irq != NO_IRQ) {
		ret = sa1111_setup_irq(sachip, pd->irq_base);
		if (ret)
			goto err_unmap;
	}

L
Linus Torvalds 已提交
803
#ifdef CONFIG_ARCH_SA1100
804 805 806
	{
	unsigned int val;

L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	/*
	 * The SDRAM configuration of the SA1110 and the SA1111 must
	 * match.  This is very important to ensure that SA1111 accesses
	 * don't corrupt the SDRAM.  Note that this ungates the SA1111's
	 * MBGNT signal, so we must have called sa1110_mb_disable()
	 * beforehand.
	 */
	sa1111_configure_smc(sachip, 1,
			     FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
			     FExtr(MDCNFG, MDCNFG_SA1110_TDL0));

	/*
	 * We only need to turn on DCLK whenever we want to use the
	 * DMA.  It can otherwise be held firmly in the off position.
	 * (currently, we always enable it.)
	 */
	val = sa1111_readl(sachip->base + SA1111_SKPCR);
	sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);

	/*
	 * Enable the SA1110 memory bus request and grant signals.
	 */
	sa1110_mb_enable();
830
	}
L
Linus Torvalds 已提交
831 832 833 834 835 836 837
#endif

	g_sa1111 = sachip;

	has_devs = ~0;
	if (machine_is_assabet() || machine_is_jornada720() ||
	    machine_is_badge4())
838
		has_devs &= ~SA1111_DEVID_PS2_MSE;
L
Linus Torvalds 已提交
839
	else
840
		has_devs &= ~SA1111_DEVID_SAC;
L
Linus Torvalds 已提交
841 842

	for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
843
		if (sa1111_devices[i].devid & has_devs)
L
Linus Torvalds 已提交
844 845 846 847
			sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);

	return 0;

848
 err_unmap:
L
Linus Torvalds 已提交
849
	iounmap(sachip->base);
850 851
 err_clk_unprep:
	clk_unprepare(sachip->clk);
852 853 854
 err_clkput:
	clk_put(sachip->clk);
 err_free:
L
Linus Torvalds 已提交
855 856 857 858
	kfree(sachip);
	return ret;
}

859 860 861 862 863 864
static int sa1111_remove_one(struct device *dev, void *data)
{
	device_unregister(dev);
	return 0;
}

L
Linus Torvalds 已提交
865 866 867 868
static void __sa1111_remove(struct sa1111 *sachip)
{
	void __iomem *irqbase = sachip->base + SA1111_INTC;

869
	device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
L
Linus Torvalds 已提交
870 871 872 873 874 875 876

	/* disable all IRQs */
	sa1111_writel(0, irqbase + SA1111_INTEN0);
	sa1111_writel(0, irqbase + SA1111_INTEN1);
	sa1111_writel(0, irqbase + SA1111_WAKEEN0);
	sa1111_writel(0, irqbase + SA1111_WAKEEN1);

877
	clk_disable(sachip->clk);
878
	clk_unprepare(sachip->clk);
879

L
Linus Torvalds 已提交
880
	if (sachip->irq != NO_IRQ) {
T
Thomas Gleixner 已提交
881 882
		irq_set_chained_handler(sachip->irq, NULL);
		irq_set_handler_data(sachip->irq, NULL);
883
		irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
L
Linus Torvalds 已提交
884 885 886 887 888

		release_mem_region(sachip->phys + SA1111_INTC, 512);
	}

	iounmap(sachip->base);
889
	clk_put(sachip->clk);
L
Linus Torvalds 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	kfree(sachip);
}

struct sa1111_save_data {
	unsigned int	skcr;
	unsigned int	skpcr;
	unsigned int	skcdr;
	unsigned char	skaud;
	unsigned char	skpwm0;
	unsigned char	skpwm1;

	/*
	 * Interrupt controller
	 */
	unsigned int	intpol0;
	unsigned int	intpol1;
	unsigned int	inten0;
	unsigned int	inten1;
	unsigned int	wakepol0;
	unsigned int	wakepol1;
	unsigned int	wakeen0;
	unsigned int	wakeen1;
};

#ifdef CONFIG_PM

916
static int sa1111_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
917
{
918
	struct sa1111 *sachip = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926
	struct sa1111_save_data *save;
	unsigned long flags;
	unsigned int val;
	void __iomem *base;

	save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
	if (!save)
		return -ENOMEM;
927
	sachip->saved_state = save;
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941

	spin_lock_irqsave(&sachip->lock, flags);

	/*
	 * Save state.
	 */
	base = sachip->base;
	save->skcr     = sa1111_readl(base + SA1111_SKCR);
	save->skpcr    = sa1111_readl(base + SA1111_SKPCR);
	save->skcdr    = sa1111_readl(base + SA1111_SKCDR);
	save->skaud    = sa1111_readl(base + SA1111_SKAUD);
	save->skpwm0   = sa1111_readl(base + SA1111_SKPWM0);
	save->skpwm1   = sa1111_readl(base + SA1111_SKPWM1);

942 943 944
	sa1111_writel(0, sachip->base + SA1111_SKPWM0);
	sa1111_writel(0, sachip->base + SA1111_SKPWM1);

L
Linus Torvalds 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	base = sachip->base + SA1111_INTC;
	save->intpol0  = sa1111_readl(base + SA1111_INTPOL0);
	save->intpol1  = sa1111_readl(base + SA1111_INTPOL1);
	save->inten0   = sa1111_readl(base + SA1111_INTEN0);
	save->inten1   = sa1111_readl(base + SA1111_INTEN1);
	save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
	save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
	save->wakeen0  = sa1111_readl(base + SA1111_WAKEEN0);
	save->wakeen1  = sa1111_readl(base + SA1111_WAKEEN1);

	/*
	 * Disable.
	 */
	val = sa1111_readl(sachip->base + SA1111_SKCR);
	sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);

961 962
	clk_disable(sachip->clk);

L
Linus Torvalds 已提交
963 964
	spin_unlock_irqrestore(&sachip->lock, flags);

965 966 967 968
#ifdef CONFIG_ARCH_SA1100
	sa1110_mb_disable();
#endif

L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979 980
	return 0;
}

/*
 *	sa1111_resume - Restore the SA1111 device state.
 *	@dev: device to restore
 *
 *	Restore the general state of the SA1111; clock control and
 *	interrupt controller.  Other parts of the SA1111 must be
 *	restored by their respective drivers, and must be called
 *	via LDM after this function.
 */
981
static int sa1111_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
982
{
983
	struct sa1111 *sachip = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
984 985 986 987
	struct sa1111_save_data *save;
	unsigned long flags, id;
	void __iomem *base;

988
	save = sachip->saved_state;
L
Linus Torvalds 已提交
989 990 991 992 993 994 995 996 997 998
	if (!save)
		return 0;

	/*
	 * Ensure that the SA1111 is still here.
	 * FIXME: shouldn't do this here.
	 */
	id = sa1111_readl(sachip->base + SA1111_SKID);
	if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
		__sa1111_remove(sachip);
999
		platform_set_drvdata(dev, NULL);
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004 1005 1006 1007
		kfree(save);
		return 0;
	}

	/*
	 * First of all, wake up the chip.
	 */
	sa1111_wake(sachip);
1008

1009 1010 1011 1012 1013
#ifdef CONFIG_ARCH_SA1100
	/* Enable the memory bus request/grant signals */
	sa1110_mb_enable();
#endif

1014 1015 1016 1017 1018 1019
	/*
	 * Only lock for write ops. Also, sa1111_wake must be called with
	 * released spinlock!
	 */
	spin_lock_irqsave(&sachip->lock, flags);

L
Linus Torvalds 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
	sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);

	base = sachip->base;
	sa1111_writel(save->skcr,     base + SA1111_SKCR);
	sa1111_writel(save->skpcr,    base + SA1111_SKPCR);
	sa1111_writel(save->skcdr,    base + SA1111_SKCDR);
	sa1111_writel(save->skaud,    base + SA1111_SKAUD);
	sa1111_writel(save->skpwm0,   base + SA1111_SKPWM0);
	sa1111_writel(save->skpwm1,   base + SA1111_SKPWM1);

	base = sachip->base + SA1111_INTC;
	sa1111_writel(save->intpol0,  base + SA1111_INTPOL0);
	sa1111_writel(save->intpol1,  base + SA1111_INTPOL1);
	sa1111_writel(save->inten0,   base + SA1111_INTEN0);
	sa1111_writel(save->inten1,   base + SA1111_INTEN1);
	sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
	sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
	sa1111_writel(save->wakeen0,  base + SA1111_WAKEEN0);
	sa1111_writel(save->wakeen1,  base + SA1111_WAKEEN1);

	spin_unlock_irqrestore(&sachip->lock, flags);

1043
	sachip->saved_state = NULL;
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	kfree(save);

	return 0;
}

#else
#define sa1111_suspend NULL
#define sa1111_resume  NULL
#endif

1054
static int __devinit sa1111_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061 1062
{
	struct resource *mem;
	int irq;

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem)
		return -EINVAL;
	irq = platform_get_irq(pdev, 0);
1063 1064
	if (irq < 0)
		return -ENXIO;
L
Linus Torvalds 已提交
1065

1066
	return __sa1111_probe(&pdev->dev, mem, irq);
L
Linus Torvalds 已提交
1067 1068
}

1069
static int sa1111_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
1070
{
1071
	struct sa1111 *sachip = platform_get_drvdata(pdev);
L
Linus Torvalds 已提交
1072 1073 1074

	if (sachip) {
#ifdef CONFIG_PM
1075 1076
		kfree(sachip->saved_state);
		sachip->saved_state = NULL;
L
Linus Torvalds 已提交
1077
#endif
J
Julia Lawall 已提交
1078 1079
		__sa1111_remove(sachip);
		platform_set_drvdata(pdev, NULL);
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	}

	return 0;
}

/*
 *	Not sure if this should be on the system bus or not yet.
 *	We really want some way to register a system device at
 *	the per-machine level, and then have this driver pick
 *	up the registered devices.
 *
 *	We also need to handle the SDRAM configuration for
 *	PXA250/SA1110 machine classes.
 */
1094
static struct platform_driver sa1111_device_driver = {
L
Linus Torvalds 已提交
1095 1096 1097 1098
	.probe		= sa1111_probe,
	.remove		= sa1111_remove,
	.suspend	= sa1111_suspend,
	.resume		= sa1111_resume,
1099 1100
	.driver		= {
		.name	= "sa1111",
1101
		.owner	= THIS_MODULE,
1102
	},
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
};

/*
 *	Get the parent device driver (us) structure
 *	from a child function device
 */
static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
{
	return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
}

/*
 * The bits in the opdiv field are non-linear.
 */
static unsigned char opdiv_table[] = { 1, 4, 2, 8 };

static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
{
	unsigned int skcdr, fbdiv, ipdiv, opdiv;

	skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);

	fbdiv = (skcdr & 0x007f) + 2;
	ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
	opdiv = opdiv_table[(skcdr & 0x3000) >> 12];

	return 3686400 * fbdiv / (ipdiv * opdiv);
}

/**
 *	sa1111_pll_clock - return the current PLL clock frequency.
 *	@sadev: SA1111 function block
 *
 *	BUG: we should look at SKCR.  We also blindly believe that
 *	the chip is being fed with the 3.6864MHz clock.
 *
 *	Returns the PLL clock in Hz.
 */
unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);

	return __sa1111_pll_clock(sachip);
}
1147
EXPORT_SYMBOL(sa1111_pll_clock);
L
Linus Torvalds 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

/**
 *	sa1111_select_audio_mode - select I2S or AC link mode
 *	@sadev: SA1111 function block
 *	@mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
 *
 *	Frob the SKCR to select AC Link mode or I2S mode for
 *	the audio block.
 */
void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;

	spin_lock_irqsave(&sachip->lock, flags);

	val = sa1111_readl(sachip->base + SA1111_SKCR);
	if (mode == SA1111_AUDIO_I2S) {
		val &= ~SKCR_SELAC;
	} else {
		val |= SKCR_SELAC;
	}
	sa1111_writel(val, sachip->base + SA1111_SKCR);

	spin_unlock_irqrestore(&sachip->lock, flags);
}
1175
EXPORT_SYMBOL(sa1111_select_audio_mode);
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

/**
 *	sa1111_set_audio_rate - set the audio sample rate
 *	@sadev: SA1111 SAC function block
 *	@rate: sample rate to select
 */
int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned int div;

	if (sadev->devid != SA1111_DEVID_SAC)
		return -EINVAL;

	div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
	if (div == 0)
		div = 1;
	if (div > 128)
		div = 128;

	sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);

	return 0;
}
1200
EXPORT_SYMBOL(sa1111_set_audio_rate);
L
Linus Torvalds 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

/**
 *	sa1111_get_audio_rate - get the audio sample rate
 *	@sadev: SA1111 SAC function block device
 */
int sa1111_get_audio_rate(struct sa1111_dev *sadev)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long div;

	if (sadev->devid != SA1111_DEVID_SAC)
		return -EINVAL;

	div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;

	return __sa1111_pll_clock(sachip) / (256 * div);
}
1218
EXPORT_SYMBOL(sa1111_get_audio_rate);
L
Linus Torvalds 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

void sa1111_set_io_dir(struct sa1111_dev *sadev,
		       unsigned int bits, unsigned int dir,
		       unsigned int sleep_dir)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;
	void __iomem *gpio = sachip->base + SA1111_GPIO;

#define MODIFY_BITS(port, mask, dir)		\
	if (mask) {				\
		val = sa1111_readl(port);	\
		val &= ~(mask);			\
		val |= (dir) & (mask);		\
		sa1111_writel(val, port);	\
	}

	spin_lock_irqsave(&sachip->lock, flags);
	MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
	MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
	MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);

	MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
	MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
	MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
	spin_unlock_irqrestore(&sachip->lock, flags);
}
1247
EXPORT_SYMBOL(sa1111_set_io_dir);
L
Linus Torvalds 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;
	void __iomem *gpio = sachip->base + SA1111_GPIO;

	spin_lock_irqsave(&sachip->lock, flags);
	MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
	MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
	MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
	spin_unlock_irqrestore(&sachip->lock, flags);
}
1262
EXPORT_SYMBOL(sa1111_set_io);
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;
	void __iomem *gpio = sachip->base + SA1111_GPIO;

	spin_lock_irqsave(&sachip->lock, flags);
	MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
	MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
	MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
	spin_unlock_irqrestore(&sachip->lock, flags);
}
1277
EXPORT_SYMBOL(sa1111_set_sleep_io);
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286

/*
 * Individual device operations.
 */

/**
 *	sa1111_enable_device - enable an on-chip SA1111 function block
 *	@sadev: SA1111 function block device to enable
 */
1287
int sa1111_enable_device(struct sa1111_dev *sadev)
L
Linus Torvalds 已提交
1288 1289 1290 1291
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;
1292
	int ret = 0;
L
Linus Torvalds 已提交
1293

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	if (sachip->pdata && sachip->pdata->enable)
		ret = sachip->pdata->enable(sachip->pdata->data, sadev->devid);

	if (ret == 0) {
		spin_lock_irqsave(&sachip->lock, flags);
		val = sa1111_readl(sachip->base + SA1111_SKPCR);
		sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
		spin_unlock_irqrestore(&sachip->lock, flags);
	}
	return ret;
L
Linus Torvalds 已提交
1304
}
1305
EXPORT_SYMBOL(sa1111_enable_device);
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

/**
 *	sa1111_disable_device - disable an on-chip SA1111 function block
 *	@sadev: SA1111 function block device to disable
 */
void sa1111_disable_device(struct sa1111_dev *sadev)
{
	struct sa1111 *sachip = sa1111_chip_driver(sadev);
	unsigned long flags;
	unsigned int val;

	spin_lock_irqsave(&sachip->lock, flags);
	val = sa1111_readl(sachip->base + SA1111_SKPCR);
	sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
	spin_unlock_irqrestore(&sachip->lock, flags);
1321 1322 1323

	if (sachip->pdata && sachip->pdata->disable)
		sachip->pdata->disable(sachip->pdata->data, sadev->devid);
L
Linus Torvalds 已提交
1324
}
1325
EXPORT_SYMBOL(sa1111_disable_device);
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

/*
 *	SA1111 "Register Access Bus."
 *
 *	We model this as a regular bus type, and hang devices directly
 *	off this.
 */
static int sa1111_match(struct device *_dev, struct device_driver *_drv)
{
	struct sa1111_dev *dev = SA1111_DEV(_dev);
	struct sa1111_driver *drv = SA1111_DRV(_drv);

1338
	return dev->devid & drv->devid;
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
}

static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
{
	struct sa1111_dev *sadev = SA1111_DEV(dev);
	struct sa1111_driver *drv = SA1111_DRV(dev->driver);
	int ret = 0;

	if (drv && drv->suspend)
		ret = drv->suspend(sadev, state);
	return ret;
}

static int sa1111_bus_resume(struct device *dev)
{
	struct sa1111_dev *sadev = SA1111_DEV(dev);
	struct sa1111_driver *drv = SA1111_DRV(dev->driver);
	int ret = 0;

	if (drv && drv->resume)
		ret = drv->resume(sadev);
	return ret;
}

1363 1364 1365 1366 1367 1368 1369 1370
static void sa1111_bus_shutdown(struct device *dev)
{
	struct sa1111_driver *drv = SA1111_DRV(dev->driver);

	if (drv && drv->shutdown)
		drv->shutdown(SA1111_DEV(dev));
}

L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
static int sa1111_bus_probe(struct device *dev)
{
	struct sa1111_dev *sadev = SA1111_DEV(dev);
	struct sa1111_driver *drv = SA1111_DRV(dev->driver);
	int ret = -ENODEV;

	if (drv->probe)
		ret = drv->probe(sadev);
	return ret;
}

static int sa1111_bus_remove(struct device *dev)
{
	struct sa1111_dev *sadev = SA1111_DEV(dev);
	struct sa1111_driver *drv = SA1111_DRV(dev->driver);
	int ret = 0;

	if (drv->remove)
		ret = drv->remove(sadev);
	return ret;
}

struct bus_type sa1111_bus_type = {
	.name		= "sa1111-rab",
	.match		= sa1111_match,
1396 1397
	.probe		= sa1111_bus_probe,
	.remove		= sa1111_bus_remove,
L
Linus Torvalds 已提交
1398 1399
	.suspend	= sa1111_bus_suspend,
	.resume		= sa1111_bus_resume,
1400
	.shutdown	= sa1111_bus_shutdown,
L
Linus Torvalds 已提交
1401
};
1402
EXPORT_SYMBOL(sa1111_bus_type);
L
Linus Torvalds 已提交
1403 1404 1405 1406 1407 1408

int sa1111_driver_register(struct sa1111_driver *driver)
{
	driver->drv.bus = &sa1111_bus_type;
	return driver_register(&driver->drv);
}
1409
EXPORT_SYMBOL(sa1111_driver_register);
L
Linus Torvalds 已提交
1410 1411 1412 1413 1414

void sa1111_driver_unregister(struct sa1111_driver *driver)
{
	driver_unregister(&driver->drv);
}
1415
EXPORT_SYMBOL(sa1111_driver_unregister);
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420

static int __init sa1111_init(void)
{
	int ret = bus_register(&sa1111_bus_type);
	if (ret == 0)
1421
		platform_driver_register(&sa1111_device_driver);
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426
	return ret;
}

static void __exit sa1111_exit(void)
{
1427
	platform_driver_unregister(&sa1111_device_driver);
L
Linus Torvalds 已提交
1428 1429 1430
	bus_unregister(&sa1111_bus_type);
}

1431
subsys_initcall(sa1111_init);
L
Linus Torvalds 已提交
1432 1433 1434 1435
module_exit(sa1111_exit);

MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
MODULE_LICENSE("GPL");