xref: /openbmc/linux/arch/arm/mach-sa1100/neponset.c (revision d2e539a5)
1 /*
2  * linux/arch/arm/mach-sa1100/neponset.c
3  */
4 #include <linux/err.h>
5 #include <linux/init.h>
6 #include <linux/ioport.h>
7 #include <linux/irq.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/serial_core.h>
12 #include <linux/slab.h>
13 
14 #include <asm/mach-types.h>
15 #include <asm/mach/map.h>
16 #include <asm/mach/serial_sa1100.h>
17 #include <asm/hardware/sa1111.h>
18 #include <asm/sizes.h>
19 
20 #include <mach/hardware.h>
21 #include <mach/assabet.h>
22 #include <mach/neponset.h>
23 
24 #define NEP_IRQ_SMC91X	0
25 #define NEP_IRQ_USAR	1
26 #define NEP_IRQ_SA1111	2
27 #define NEP_IRQ_NR	3
28 
29 struct neponset_drvdata {
30 	struct platform_device *sa1111;
31 	struct platform_device *smc91x;
32 	unsigned irq_base;
33 #ifdef CONFIG_PM_SLEEP
34 	u32 ncr0;
35 	u32 mdm_ctl_0;
36 #endif
37 };
38 
39 void neponset_ncr_frob(unsigned int mask, unsigned int val)
40 {
41 	unsigned long flags;
42 
43 	local_irq_save(flags);
44 	NCR_0 = (NCR_0 & ~mask) | val;
45 	local_irq_restore(flags);
46 }
47 
48 static void neponset_set_mctrl(struct uart_port *port, u_int mctrl)
49 {
50 	u_int mdm_ctl0 = MDM_CTL_0;
51 
52 	if (port->mapbase == _Ser1UTCR0) {
53 		if (mctrl & TIOCM_RTS)
54 			mdm_ctl0 &= ~MDM_CTL0_RTS2;
55 		else
56 			mdm_ctl0 |= MDM_CTL0_RTS2;
57 
58 		if (mctrl & TIOCM_DTR)
59 			mdm_ctl0 &= ~MDM_CTL0_DTR2;
60 		else
61 			mdm_ctl0 |= MDM_CTL0_DTR2;
62 	} else if (port->mapbase == _Ser3UTCR0) {
63 		if (mctrl & TIOCM_RTS)
64 			mdm_ctl0 &= ~MDM_CTL0_RTS1;
65 		else
66 			mdm_ctl0 |= MDM_CTL0_RTS1;
67 
68 		if (mctrl & TIOCM_DTR)
69 			mdm_ctl0 &= ~MDM_CTL0_DTR1;
70 		else
71 			mdm_ctl0 |= MDM_CTL0_DTR1;
72 	}
73 
74 	MDM_CTL_0 = mdm_ctl0;
75 }
76 
77 static u_int neponset_get_mctrl(struct uart_port *port)
78 {
79 	u_int ret = TIOCM_CD | TIOCM_CTS | TIOCM_DSR;
80 	u_int mdm_ctl1 = MDM_CTL_1;
81 
82 	if (port->mapbase == _Ser1UTCR0) {
83 		if (mdm_ctl1 & MDM_CTL1_DCD2)
84 			ret &= ~TIOCM_CD;
85 		if (mdm_ctl1 & MDM_CTL1_CTS2)
86 			ret &= ~TIOCM_CTS;
87 		if (mdm_ctl1 & MDM_CTL1_DSR2)
88 			ret &= ~TIOCM_DSR;
89 	} else if (port->mapbase == _Ser3UTCR0) {
90 		if (mdm_ctl1 & MDM_CTL1_DCD1)
91 			ret &= ~TIOCM_CD;
92 		if (mdm_ctl1 & MDM_CTL1_CTS1)
93 			ret &= ~TIOCM_CTS;
94 		if (mdm_ctl1 & MDM_CTL1_DSR1)
95 			ret &= ~TIOCM_DSR;
96 	}
97 
98 	return ret;
99 }
100 
101 static struct sa1100_port_fns neponset_port_fns __devinitdata = {
102 	.set_mctrl	= neponset_set_mctrl,
103 	.get_mctrl	= neponset_get_mctrl,
104 };
105 
106 /*
107  * Install handler for Neponset IRQ.  Note that we have to loop here
108  * since the ETHERNET and USAR IRQs are level based, and we need to
109  * ensure that the IRQ signal is deasserted before returning.  This
110  * is rather unfortunate.
111  */
112 static void neponset_irq_handler(unsigned int irq, struct irq_desc *desc)
113 {
114 	struct neponset_drvdata *d = irq_desc_get_handler_data(desc);
115 	unsigned int irr;
116 
117 	while (1) {
118 		/*
119 		 * Acknowledge the parent IRQ.
120 		 */
121 		desc->irq_data.chip->irq_ack(&desc->irq_data);
122 
123 		/*
124 		 * Read the interrupt reason register.  Let's have all
125 		 * active IRQ bits high.  Note: there is a typo in the
126 		 * Neponset user's guide for the SA1111 IRR level.
127 		 */
128 		irr = IRR ^ (IRR_ETHERNET | IRR_USAR);
129 
130 		if ((irr & (IRR_ETHERNET | IRR_USAR | IRR_SA1111)) == 0)
131 			break;
132 
133 		/*
134 		 * Since there is no individual mask, we have to
135 		 * mask the parent IRQ.  This is safe, since we'll
136 		 * recheck the register for any pending IRQs.
137 		 */
138 		if (irr & (IRR_ETHERNET | IRR_USAR)) {
139 			desc->irq_data.chip->irq_mask(&desc->irq_data);
140 
141 			/*
142 			 * Ack the interrupt now to prevent re-entering
143 			 * this neponset handler.  Again, this is safe
144 			 * since we'll check the IRR register prior to
145 			 * leaving.
146 			 */
147 			desc->irq_data.chip->irq_ack(&desc->irq_data);
148 
149 			if (irr & IRR_ETHERNET)
150 				generic_handle_irq(d->irq_base + NEP_IRQ_SMC91X);
151 
152 			if (irr & IRR_USAR)
153 				generic_handle_irq(d->irq_base + NEP_IRQ_USAR);
154 
155 			desc->irq_data.chip->irq_unmask(&desc->irq_data);
156 		}
157 
158 		if (irr & IRR_SA1111)
159 			generic_handle_irq(d->irq_base + NEP_IRQ_SA1111);
160 	}
161 }
162 
163 /* Yes, we really do not have any kind of masking or unmasking */
164 static void nochip_noop(struct irq_data *irq)
165 {
166 }
167 
168 static struct irq_chip nochip = {
169 	.name = "neponset",
170 	.irq_ack = nochip_noop,
171 	.irq_mask = nochip_noop,
172 	.irq_unmask = nochip_noop,
173 };
174 
175 static struct sa1111_platform_data sa1111_info = {
176 	.irq_base	= IRQ_BOARD_END,
177 };
178 
179 static int __devinit neponset_probe(struct platform_device *dev)
180 {
181 	struct neponset_drvdata *d;
182 	struct resource *sa1111_res, *smc91x_res;
183 	struct resource sa1111_resources[] = {
184 		DEFINE_RES_MEM(0x40000000, SZ_8K),
185 		{ .flags = IORESOURCE_IRQ },
186 	};
187 	struct platform_device_info sa1111_devinfo = {
188 		.parent = &dev->dev,
189 		.name = "sa1111",
190 		.id = 0,
191 		.res = sa1111_resources,
192 		.num_res = ARRAY_SIZE(sa1111_resources),
193 		.data = &sa1111_info,
194 		.size_data = sizeof(sa1111_info),
195 		.dma_mask = 0xffffffffUL,
196 	};
197 	struct resource smc91x_resources[] = {
198 		DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS,
199 			0x02000000, "smc91x-regs"),
200 		DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS + 0x02000000,
201 			0x02000000, "smc91x-attrib"),
202 		{ .flags = IORESOURCE_IRQ },
203 	};
204 	struct platform_device_info smc91x_devinfo = {
205 		.parent = &dev->dev,
206 		.name = "smc91x",
207 		.id = 0,
208 		.res = smc91x_resources,
209 		.num_res = ARRAY_SIZE(smc91x_resources),
210 	};
211 	int ret, irq;
212 
213 	irq = ret = platform_get_irq(dev, 0);
214 	if (ret < 0)
215 		goto err_alloc;
216 
217 	smc91x_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
218 	sa1111_res = platform_get_resource(dev, IORESOURCE_MEM, 2);
219 	if (!smc91x_res || !sa1111_res) {
220 		ret = -ENXIO;
221 		goto err_alloc;
222 	}
223 
224 	d = kzalloc(sizeof(*d), GFP_KERNEL);
225 	if (!d) {
226 		ret = -ENOMEM;
227 		goto err_alloc;
228 	}
229 
230 	ret = irq_alloc_descs(-1, IRQ_BOARD_START, NEP_IRQ_NR, -1);
231 	if (ret <= 0) {
232 		dev_err(&dev->dev, "unable to allocate %u irqs: %d\n",
233 			NEP_IRQ_NR, ret);
234 		if (ret == 0)
235 			ret = -ENOMEM;
236 		goto err_irq_alloc;
237 	}
238 
239 	d->irq_base = ret;
240 
241 	irq_set_chip_and_handler(d->irq_base + NEP_IRQ_SMC91X, &nochip,
242 		handle_simple_irq);
243 	set_irq_flags(d->irq_base + NEP_IRQ_SMC91X, IRQF_VALID | IRQF_PROBE);
244 	irq_set_chip_and_handler(d->irq_base + NEP_IRQ_USAR, &nochip,
245 		handle_simple_irq);
246 	set_irq_flags(d->irq_base + NEP_IRQ_USAR, IRQF_VALID | IRQF_PROBE);
247 	irq_set_chip(d->irq_base + NEP_IRQ_SA1111, &nochip);
248 
249 	irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
250 	irq_set_handler_data(irq, d);
251 	irq_set_chained_handler(irq, neponset_irq_handler);
252 
253 	/*
254 	 * We would set IRQ_GPIO25 to be a wake-up IRQ, but unfortunately
255 	 * something on the Neponset activates this IRQ on sleep (eth?)
256 	 */
257 #if 0
258 	enable_irq_wake(irq);
259 #endif
260 
261 	dev_info(&dev->dev, "Neponset daughter board, providing IRQ%u-%u\n",
262 		 d->irq_base, d->irq_base + NEP_IRQ_NR - 1);
263 
264 	sa1100_register_uart_fns(&neponset_port_fns);
265 
266 	/* Disable GPIO 0/1 drivers so the buttons work on the Assabet */
267 	NCR_0 = NCR_GP01_OFF;
268 
269 	sa1111_resources[0].parent = sa1111_res;
270 	sa1111_resources[1].start = d->irq_base + NEP_IRQ_SA1111;
271 	sa1111_resources[1].end = d->irq_base + NEP_IRQ_SA1111;
272 	d->sa1111 = platform_device_register_full(&sa1111_devinfo);
273 
274 	smc91x_resources[0].parent = smc91x_res;
275 	smc91x_resources[1].parent = smc91x_res;
276 	smc91x_resources[2].start = d->irq_base + NEP_IRQ_SMC91X;
277 	smc91x_resources[2].end = d->irq_base + NEP_IRQ_SMC91X;
278 	d->smc91x = platform_device_register_full(&smc91x_devinfo);
279 
280 	platform_set_drvdata(dev, d);
281 
282 	return 0;
283 
284  err_irq_alloc:
285 	kfree(d);
286  err_alloc:
287 	return ret;
288 }
289 
290 static int __devexit neponset_remove(struct platform_device *dev)
291 {
292 	struct neponset_drvdata *d = platform_get_drvdata(dev);
293 	int irq = platform_get_irq(dev, 0);
294 
295 	if (!IS_ERR(d->sa1111))
296 		platform_device_unregister(d->sa1111);
297 	if (!IS_ERR(d->smc91x))
298 		platform_device_unregister(d->smc91x);
299 	irq_set_chained_handler(irq, NULL);
300 	irq_free_descs(d->irq_base, NEP_IRQ_NR);
301 	kfree(d);
302 
303 	return 0;
304 }
305 
306 #ifdef CONFIG_PM
307 static int neponset_suspend(struct platform_device *dev, pm_message_t state)
308 {
309 	struct neponset_drvdata *d = platform_get_drvdata(dev);
310 
311 	d->ncr0 = NCR_0;
312 	d->mdm_ctl_0 = MDM_CTL_0;
313 
314 	return 0;
315 }
316 
317 static int neponset_resume(struct platform_device *dev)
318 {
319 	struct neponset_drvdata *d = platform_get_drvdata(dev);
320 
321 	NCR_0 = d->ncr0;
322 	MDM_CTL_0 = d->mdm_ctl_0;
323 
324 	return 0;
325 }
326 
327 #else
328 #define neponset_suspend NULL
329 #define neponset_resume  NULL
330 #endif
331 
332 static struct platform_driver neponset_device_driver = {
333 	.probe		= neponset_probe,
334 	.remove		= __devexit_p(neponset_remove),
335 	.suspend	= neponset_suspend,
336 	.resume		= neponset_resume,
337 	.driver		= {
338 		.name	= "neponset",
339 		.owner	= THIS_MODULE,
340 	},
341 };
342 
343 static struct resource neponset_resources[] = {
344 	DEFINE_RES_MEM(0x10000000, 0x08000000),
345 	DEFINE_RES_MEM(0x18000000, 0x04000000),
346 	DEFINE_RES_MEM(0x40000000, SZ_8K),
347 	DEFINE_RES_IRQ(IRQ_GPIO25),
348 };
349 
350 static struct platform_device neponset_device = {
351 	.name		= "neponset",
352 	.id		= 0,
353 	.num_resources	= ARRAY_SIZE(neponset_resources),
354 	.resource	= neponset_resources,
355 };
356 
357 extern void sa1110_mb_disable(void);
358 
359 static int __init neponset_init(void)
360 {
361 	platform_driver_register(&neponset_device_driver);
362 
363 	/*
364 	 * The Neponset is only present on the Assabet machine type.
365 	 */
366 	if (!machine_is_assabet())
367 		return -ENODEV;
368 
369 	/*
370 	 * Ensure that the memory bus request/grant signals are setup,
371 	 * and the grant is held in its inactive state, whether or not
372 	 * we actually have a Neponset attached.
373 	 */
374 	sa1110_mb_disable();
375 
376 	if (!machine_has_neponset()) {
377 		printk(KERN_DEBUG "Neponset expansion board not present\n");
378 		return -ENODEV;
379 	}
380 
381 	if (WHOAMI != 0x11) {
382 		printk(KERN_WARNING "Neponset board detected, but "
383 			"wrong ID: %02x\n", WHOAMI);
384 		return -ENODEV;
385 	}
386 
387 	return platform_device_register(&neponset_device);
388 }
389 
390 subsys_initcall(neponset_init);
391 
392 static struct map_desc neponset_io_desc[] __initdata = {
393 	{	/* System Registers */
394 		.virtual	=  0xf3000000,
395 		.pfn		= __phys_to_pfn(0x10000000),
396 		.length		= SZ_1M,
397 		.type		= MT_DEVICE
398 	}, {	/* SA-1111 */
399 		.virtual	=  0xf4000000,
400 		.pfn		= __phys_to_pfn(0x40000000),
401 		.length		= SZ_1M,
402 		.type		= MT_DEVICE
403 	}
404 };
405 
406 void __init neponset_map_io(void)
407 {
408 	iotable_init(neponset_io_desc, ARRAY_SIZE(neponset_io_desc));
409 }
410