neponset.c 10.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 * linux/arch/arm/mach-sa1100/neponset.c
 */
4
#include <linux/err.h>
Linus Torvalds's avatar
Linus Torvalds committed
5 6
#include <linux/init.h>
#include <linux/ioport.h>
7
#include <linux/irq.h>
8
#include <linux/kernel.h>
9
#include <linux/module.h>
10
#include <linux/platform_data/sa11x0-serial.h>
11
#include <linux/platform_device.h>
12
#include <linux/pm.h>
13
#include <linux/serial_core.h>
14
#include <linux/slab.h>
15
#include <linux/smc91x.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21

#include <asm/mach-types.h>
#include <asm/mach/map.h>
#include <asm/hardware/sa1111.h>
#include <asm/sizes.h>

22 23 24
#include <mach/hardware.h>
#include <mach/assabet.h>
#include <mach/neponset.h>
25
#include <mach/irqs.h>
26

27 28 29 30 31
#define NEP_IRQ_SMC91X	0
#define NEP_IRQ_USAR	1
#define NEP_IRQ_SA1111	2
#define NEP_IRQ_NR	3

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define WHOAMI		0x00
#define LEDS		0x10
#define SWPK		0x20
#define IRR		0x24
#define KP_Y_IN		0x80
#define KP_X_OUT	0x90
#define NCR_0		0xa0
#define MDM_CTL_0	0xb0
#define MDM_CTL_1	0xb4
#define AUD_CTL		0xc0

#define IRR_ETHERNET	(1 << 0)
#define IRR_USAR	(1 << 1)
#define IRR_SA1111	(1 << 2)

#define MDM_CTL0_RTS1	(1 << 0)
#define MDM_CTL0_DTR1	(1 << 1)
#define MDM_CTL0_RTS2	(1 << 2)
#define MDM_CTL0_DTR2	(1 << 3)

#define MDM_CTL1_CTS1	(1 << 0)
#define MDM_CTL1_DSR1	(1 << 1)
#define MDM_CTL1_DCD1	(1 << 2)
#define MDM_CTL1_CTS2	(1 << 3)
#define MDM_CTL1_DSR2	(1 << 4)
#define MDM_CTL1_DCD2	(1 << 5)

#define AUD_SEL_1341	(1 << 0)
#define AUD_MUTE_1341	(1 << 1)

62 63
extern void sa1110_mb_disable(void);

64
struct neponset_drvdata {
65
	void __iomem *base;
66 67
	struct platform_device *sa1111;
	struct platform_device *smc91x;
68
	unsigned irq_base;
69 70 71 72 73 74
#ifdef CONFIG_PM_SLEEP
	u32 ncr0;
	u32 mdm_ctl_0;
#endif
};

75 76
static void __iomem *nep_base;

77 78
void neponset_ncr_frob(unsigned int mask, unsigned int val)
{
79 80 81 82 83 84 85 86 87 88 89 90 91
	void __iomem *base = nep_base;

	if (base) {
		unsigned long flags;
		unsigned v;

		local_irq_save(flags);
		v = readb_relaxed(base + NCR_0);
		writeb_relaxed((v & ~mask) | val, base + NCR_0);
		local_irq_restore(flags);
	} else {
		WARN(1, "nep_base unset\n");
	}
92
}
93
EXPORT_SYMBOL(neponset_ncr_frob);
94

95 96
static void neponset_set_mctrl(struct uart_port *port, u_int mctrl)
{
97 98 99 100 101
	void __iomem *base = nep_base;
	u_int mdm_ctl0;

	if (!base)
		return;
102

103
	mdm_ctl0 = readb_relaxed(base + MDM_CTL_0);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	if (port->mapbase == _Ser1UTCR0) {
		if (mctrl & TIOCM_RTS)
			mdm_ctl0 &= ~MDM_CTL0_RTS2;
		else
			mdm_ctl0 |= MDM_CTL0_RTS2;

		if (mctrl & TIOCM_DTR)
			mdm_ctl0 &= ~MDM_CTL0_DTR2;
		else
			mdm_ctl0 |= MDM_CTL0_DTR2;
	} else if (port->mapbase == _Ser3UTCR0) {
		if (mctrl & TIOCM_RTS)
			mdm_ctl0 &= ~MDM_CTL0_RTS1;
		else
			mdm_ctl0 |= MDM_CTL0_RTS1;

		if (mctrl & TIOCM_DTR)
			mdm_ctl0 &= ~MDM_CTL0_DTR1;
		else
			mdm_ctl0 |= MDM_CTL0_DTR1;
	}

126
	writeb_relaxed(mdm_ctl0, base + MDM_CTL_0);
127 128 129 130
}

static u_int neponset_get_mctrl(struct uart_port *port)
{
131
	void __iomem *base = nep_base;
132
	u_int ret = TIOCM_CD | TIOCM_CTS | TIOCM_DSR;
133
	u_int mdm_ctl1;
134

135 136 137 138
	if (!base)
		return ret;

	mdm_ctl1 = readb_relaxed(base + MDM_CTL_1);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	if (port->mapbase == _Ser1UTCR0) {
		if (mdm_ctl1 & MDM_CTL1_DCD2)
			ret &= ~TIOCM_CD;
		if (mdm_ctl1 & MDM_CTL1_CTS2)
			ret &= ~TIOCM_CTS;
		if (mdm_ctl1 & MDM_CTL1_DSR2)
			ret &= ~TIOCM_DSR;
	} else if (port->mapbase == _Ser3UTCR0) {
		if (mdm_ctl1 & MDM_CTL1_DCD1)
			ret &= ~TIOCM_CD;
		if (mdm_ctl1 & MDM_CTL1_CTS1)
			ret &= ~TIOCM_CTS;
		if (mdm_ctl1 & MDM_CTL1_DSR1)
			ret &= ~TIOCM_DSR;
	}

	return ret;
}

158
static struct sa1100_port_fns neponset_port_fns = {
159 160 161 162
	.set_mctrl	= neponset_set_mctrl,
	.get_mctrl	= neponset_get_mctrl,
};

Linus Torvalds's avatar
Linus Torvalds committed
163 164 165 166 167 168
/*
 * Install handler for Neponset IRQ.  Note that we have to loop here
 * since the ETHERNET and USAR IRQs are level based, and we need to
 * ensure that the IRQ signal is deasserted before returning.  This
 * is rather unfortunate.
 */
169
static void neponset_irq_handler(struct irq_desc *desc)
Linus Torvalds's avatar
Linus Torvalds committed
170
{
171
	struct neponset_drvdata *d = irq_desc_get_handler_data(desc);
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177
	unsigned int irr;

	while (1) {
		/*
		 * Acknowledge the parent IRQ.
		 */
178
		desc->irq_data.chip->irq_ack(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181 182 183 184

		/*
		 * Read the interrupt reason register.  Let's have all
		 * active IRQ bits high.  Note: there is a typo in the
		 * Neponset user's guide for the SA1111 IRR level.
		 */
185 186
		irr = readb_relaxed(d->base + IRR);
		irr ^= IRR_ETHERNET | IRR_USAR;
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194 195 196

		if ((irr & (IRR_ETHERNET | IRR_USAR | IRR_SA1111)) == 0)
			break;

		/*
		 * Since there is no individual mask, we have to
		 * mask the parent IRQ.  This is safe, since we'll
		 * recheck the register for any pending IRQs.
		 */
		if (irr & (IRR_ETHERNET | IRR_USAR)) {
197
			desc->irq_data.chip->irq_mask(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
198

Russell King's avatar
Russell King committed
199 200 201 202 203 204
			/*
			 * Ack the interrupt now to prevent re-entering
			 * this neponset handler.  Again, this is safe
			 * since we'll check the IRR register prior to
			 * leaving.
			 */
205
			desc->irq_data.chip->irq_ack(&desc->irq_data);
Russell King's avatar
Russell King committed
206

207 208
			if (irr & IRR_ETHERNET)
				generic_handle_irq(d->irq_base + NEP_IRQ_SMC91X);
Linus Torvalds's avatar
Linus Torvalds committed
209

210 211
			if (irr & IRR_USAR)
				generic_handle_irq(d->irq_base + NEP_IRQ_USAR);
Linus Torvalds's avatar
Linus Torvalds committed
212

213
			desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
214 215
		}

216 217
		if (irr & IRR_SA1111)
			generic_handle_irq(d->irq_base + NEP_IRQ_SA1111);
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220
	}
}

221
/* Yes, we really do not have any kind of masking or unmasking */
222 223 224 225 226 227 228 229 230 231 232
static void nochip_noop(struct irq_data *irq)
{
}

static struct irq_chip nochip = {
	.name = "neponset",
	.irq_ack = nochip_noop,
	.irq_mask = nochip_noop,
	.irq_unmask = nochip_noop,
};

233
static struct sa1111_platform_data sa1111_info = {
234
	.disable_devs	= SA1111_DEVID_PS2_MSE,
235 236
};

237
static int neponset_probe(struct platform_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
238
{
239
	struct neponset_drvdata *d;
240
	struct resource *nep_res, *sa1111_res, *smc91x_res;
241 242 243 244
	struct resource sa1111_resources[] = {
		DEFINE_RES_MEM(0x40000000, SZ_8K),
		{ .flags = IORESOURCE_IRQ },
	};
245 246 247 248 249 250 251 252 253 254
	struct platform_device_info sa1111_devinfo = {
		.parent = &dev->dev,
		.name = "sa1111",
		.id = 0,
		.res = sa1111_resources,
		.num_res = ARRAY_SIZE(sa1111_resources),
		.data = &sa1111_info,
		.size_data = sizeof(sa1111_info),
		.dma_mask = 0xffffffffUL,
	};
255 256 257 258 259 260 261
	struct resource smc91x_resources[] = {
		DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS,
			0x02000000, "smc91x-regs"),
		DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS + 0x02000000,
			0x02000000, "smc91x-attrib"),
		{ .flags = IORESOURCE_IRQ },
	};
262 263 264
	struct smc91x_platdata smc91x_platdata = {
		.flags = SMC91X_USE_8BIT | SMC91X_IO_SHIFT_2 | SMC91X_NOWAIT,
	};
265 266 267 268 269 270
	struct platform_device_info smc91x_devinfo = {
		.parent = &dev->dev,
		.name = "smc91x",
		.id = 0,
		.res = smc91x_resources,
		.num_res = ARRAY_SIZE(smc91x_resources),
271 272
		.data = &smc91x_platdata,
		.size_data = sizeof(smc91x_platdata),
273
	};
274 275
	int ret, irq;

276 277 278
	if (nep_base)
		return -EBUSY;

279 280 281
	irq = ret = platform_get_irq(dev, 0);
	if (ret < 0)
		goto err_alloc;
282

283
	nep_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
284 285
	smc91x_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
	sa1111_res = platform_get_resource(dev, IORESOURCE_MEM, 2);
286
	if (!nep_res || !smc91x_res || !sa1111_res) {
287 288 289 290
		ret = -ENXIO;
		goto err_alloc;
	}

291 292 293 294 295 296
	d = kzalloc(sizeof(*d), GFP_KERNEL);
	if (!d) {
		ret = -ENOMEM;
		goto err_alloc;
	}

297 298 299 300 301 302 303 304 305 306 307 308 309
	d->base = ioremap(nep_res->start, SZ_4K);
	if (!d->base) {
		ret = -ENOMEM;
		goto err_ioremap;
	}

	if (readb_relaxed(d->base + WHOAMI) != 0x11) {
		dev_warn(&dev->dev, "Neponset board detected, but wrong ID: %02x\n",
			 readb_relaxed(d->base + WHOAMI));
		ret = -ENODEV;
		goto err_id;
	}

310 311 312 313 314 315 316 317 318 319 320 321 322
	ret = irq_alloc_descs(-1, IRQ_BOARD_START, NEP_IRQ_NR, -1);
	if (ret <= 0) {
		dev_err(&dev->dev, "unable to allocate %u irqs: %d\n",
			NEP_IRQ_NR, ret);
		if (ret == 0)
			ret = -ENOMEM;
		goto err_irq_alloc;
	}

	d->irq_base = ret;

	irq_set_chip_and_handler(d->irq_base + NEP_IRQ_SMC91X, &nochip,
		handle_simple_irq);
323
	irq_clear_status_flags(d->irq_base + NEP_IRQ_SMC91X, IRQ_NOREQUEST | IRQ_NOPROBE);
324 325
	irq_set_chip_and_handler(d->irq_base + NEP_IRQ_USAR, &nochip,
		handle_simple_irq);
326
	irq_clear_status_flags(d->irq_base + NEP_IRQ_USAR, IRQ_NOREQUEST | IRQ_NOPROBE);
327
	irq_set_chip(d->irq_base + NEP_IRQ_SA1111, &nochip);
Linus Torvalds's avatar
Linus Torvalds committed
328

329
	irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
330
	irq_set_chained_handler_and_data(irq, neponset_irq_handler, d);
Linus Torvalds's avatar
Linus Torvalds committed
331 332

	/*
333 334
	 * We would set IRQ_GPIO25 to be a wake-up IRQ, but unfortunately
	 * something on the Neponset activates this IRQ on sleep (eth?)
Linus Torvalds's avatar
Linus Torvalds committed
335 336
	 */
#if 0
337
	enable_irq_wake(irq);
Linus Torvalds's avatar
Linus Torvalds committed
338 339
#endif

340 341
	dev_info(&dev->dev, "Neponset daughter board, providing IRQ%u-%u\n",
		 d->irq_base, d->irq_base + NEP_IRQ_NR - 1);
342
	nep_base = d->base;
Linus Torvalds's avatar
Linus Torvalds committed
343

344 345
	sa1100_register_uart_fns(&neponset_port_fns);

346 347 348
	/* Ensure that the memory bus request/grant signals are setup */
	sa1110_mb_disable();

349
	/* Disable GPIO 0/1 drivers so the buttons work on the Assabet */
350
	writeb_relaxed(NCR_GP01_OFF, d->base + NCR_0);
Linus Torvalds's avatar
Linus Torvalds committed
351

352
	sa1111_resources[0].parent = sa1111_res;
353 354
	sa1111_resources[1].start = d->irq_base + NEP_IRQ_SA1111;
	sa1111_resources[1].end = d->irq_base + NEP_IRQ_SA1111;
355
	d->sa1111 = platform_device_register_full(&sa1111_devinfo);
356

357 358
	smc91x_resources[0].parent = smc91x_res;
	smc91x_resources[1].parent = smc91x_res;
359 360
	smc91x_resources[2].start = d->irq_base + NEP_IRQ_SMC91X;
	smc91x_resources[2].end = d->irq_base + NEP_IRQ_SMC91X;
361 362
	d->smc91x = platform_device_register_full(&smc91x_devinfo);

363 364
	platform_set_drvdata(dev, d);

Linus Torvalds's avatar
Linus Torvalds committed
365
	return 0;
366

367
 err_irq_alloc:
368 369 370
 err_id:
	iounmap(d->base);
 err_ioremap:
371
	kfree(d);
372 373
 err_alloc:
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
374 375
}

376
static int neponset_remove(struct platform_device *dev)
377 378
{
	struct neponset_drvdata *d = platform_get_drvdata(dev);
379
	int irq = platform_get_irq(dev, 0);
Linus Torvalds's avatar
Linus Torvalds committed
380

381 382 383 384
	if (!IS_ERR(d->sa1111))
		platform_device_unregister(d->sa1111);
	if (!IS_ERR(d->smc91x))
		platform_device_unregister(d->smc91x);
385
	irq_set_chained_handler(irq, NULL);
386
	irq_free_descs(d->irq_base, NEP_IRQ_NR);
387 388
	nep_base = NULL;
	iounmap(d->base);
389 390 391 392
	kfree(d);

	return 0;
}
393

394 395
#ifdef CONFIG_PM_SLEEP
static int neponset_suspend(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
396
{
397
	struct neponset_drvdata *d = dev_get_drvdata(dev);
398

399 400
	d->ncr0 = readb_relaxed(d->base + NCR_0);
	d->mdm_ctl_0 = readb_relaxed(d->base + MDM_CTL_0);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404

	return 0;
}

405
static int neponset_resume(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
406
{
407
	struct neponset_drvdata *d = dev_get_drvdata(dev);
408

409 410
	writeb_relaxed(d->ncr0, d->base + NCR_0);
	writeb_relaxed(d->mdm_ctl_0, d->base + MDM_CTL_0);
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414

	return 0;
}

415 416 417 418 419 420 421
static const struct dev_pm_ops neponset_pm_ops = {
	.suspend_noirq = neponset_suspend,
	.resume_noirq = neponset_resume,
	.freeze_noirq = neponset_suspend,
	.restore_noirq = neponset_resume,
};
#define PM_OPS &neponset_pm_ops
Linus Torvalds's avatar
Linus Torvalds committed
422
#else
423
#define PM_OPS NULL
Linus Torvalds's avatar
Linus Torvalds committed
424 425
#endif

426
static struct platform_driver neponset_device_driver = {
Linus Torvalds's avatar
Linus Torvalds committed
427
	.probe		= neponset_probe,
428
	.remove		= neponset_remove,
429 430
	.driver		= {
		.name	= "neponset",
431
		.pm	= PM_OPS,
432
	},
Linus Torvalds's avatar
Linus Torvalds committed
433 434 435 436
};

static int __init neponset_init(void)
{
437
	return platform_driver_register(&neponset_device_driver);
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440
}

subsys_initcall(neponset_init);