blob: d3a8f5e264871d1f6361397f2bb901255203fe00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/arch/arm/common/sa1111.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * SA1111 support
5 *
6 * Original code by John Dorsey
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This file contains all generic SA1111 support.
13 *
14 * All initialization functions provided here are intended to be called
15 * from machine specific code with proper arguments when required.
16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010023#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/dma-mapping.h>
Russell King97d654f2006-03-15 15:54:37 +000027#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010028#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/mach-types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/irq.h>
33#include <asm/mach/irq.h>
Russell King45e109d2005-11-16 18:29:51 +000034#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/hardware/sa1111.h>
37
Eric Miao19851c52009-12-26 16:23:02 +080038/* SA1111 IRQs */
39#define IRQ_GPAIN0 (0)
40#define IRQ_GPAIN1 (1)
41#define IRQ_GPAIN2 (2)
42#define IRQ_GPAIN3 (3)
43#define IRQ_GPBIN0 (4)
44#define IRQ_GPBIN1 (5)
45#define IRQ_GPBIN2 (6)
46#define IRQ_GPBIN3 (7)
47#define IRQ_GPBIN4 (8)
48#define IRQ_GPBIN5 (9)
49#define IRQ_GPCIN0 (10)
50#define IRQ_GPCIN1 (11)
51#define IRQ_GPCIN2 (12)
52#define IRQ_GPCIN3 (13)
53#define IRQ_GPCIN4 (14)
54#define IRQ_GPCIN5 (15)
55#define IRQ_GPCIN6 (16)
56#define IRQ_GPCIN7 (17)
57#define IRQ_MSTXINT (18)
58#define IRQ_MSRXINT (19)
59#define IRQ_MSSTOPERRINT (20)
60#define IRQ_TPTXINT (21)
61#define IRQ_TPRXINT (22)
62#define IRQ_TPSTOPERRINT (23)
63#define SSPXMTINT (24)
64#define SSPRCVINT (25)
65#define SSPROR (26)
66#define AUDXMTDMADONEA (32)
67#define AUDRCVDMADONEA (33)
68#define AUDXMTDMADONEB (34)
69#define AUDRCVDMADONEB (35)
70#define AUDTFSR (36)
71#define AUDRFSR (37)
72#define AUDTUR (38)
73#define AUDROR (39)
74#define AUDDTS (40)
75#define AUDRDD (41)
76#define AUDSTO (42)
77#define IRQ_USBPWR (43)
78#define IRQ_HCIM (44)
79#define IRQ_HCIBUFFACC (45)
80#define IRQ_HCIRMTWKP (46)
81#define IRQ_NHCIMFCIR (47)
82#define IRQ_USB_PORT_RESUME (48)
83#define IRQ_S0_READY_NINT (49)
84#define IRQ_S1_READY_NINT (50)
85#define IRQ_S0_CD_VALID (51)
86#define IRQ_S1_CD_VALID (52)
87#define IRQ_S0_BVD1_STSCHG (53)
88#define IRQ_S1_BVD1_STSCHG (54)
89
Russell King29c140b2012-02-03 19:03:31 +000090extern void sa1110_mb_enable(void);
91extern void sa1110_mb_disable(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * We keep the following data for the overall SA1111. Note that the
95 * struct device and struct resource are "fake"; they should be supplied
96 * by the bus above us. However, in the interests of getting all SA1111
97 * drivers converted over to the device model, we provide this as an
98 * anchor point for all the other drivers.
99 */
100struct sa1111 {
101 struct device *dev;
Russell King97d654f2006-03-15 15:54:37 +0000102 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned long phys;
104 int irq;
Eric Miao19851c52009-12-26 16:23:02 +0800105 int irq_base; /* base for cascaded on-chip IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 spinlock_t lock;
107 void __iomem *base;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700108#ifdef CONFIG_PM
109 void *saved_state;
110#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113/*
114 * We _really_ need to eliminate this. Its only users
115 * are the PWM and DMA checking code.
116 */
117static struct sa1111 *g_sa1111;
118
119struct sa1111_dev_info {
120 unsigned long offset;
121 unsigned long skpcr_mask;
122 unsigned int devid;
123 unsigned int irq[6];
124};
125
126static struct sa1111_dev_info sa1111_devices[] = {
127 {
128 .offset = SA1111_USB,
129 .skpcr_mask = SKPCR_UCLKEN,
130 .devid = SA1111_DEVID_USB,
131 .irq = {
132 IRQ_USBPWR,
133 IRQ_HCIM,
134 IRQ_HCIBUFFACC,
135 IRQ_HCIRMTWKP,
136 IRQ_NHCIMFCIR,
137 IRQ_USB_PORT_RESUME
138 },
139 },
140 {
141 .offset = 0x0600,
142 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
143 .devid = SA1111_DEVID_SAC,
144 .irq = {
145 AUDXMTDMADONEA,
146 AUDXMTDMADONEB,
147 AUDRCVDMADONEA,
148 AUDRCVDMADONEB
149 },
150 },
151 {
152 .offset = 0x0800,
153 .skpcr_mask = SKPCR_SCLKEN,
154 .devid = SA1111_DEVID_SSP,
155 },
156 {
157 .offset = SA1111_KBD,
158 .skpcr_mask = SKPCR_PTCLKEN,
159 .devid = SA1111_DEVID_PS2,
160 .irq = {
161 IRQ_TPRXINT,
162 IRQ_TPTXINT
163 },
164 },
165 {
166 .offset = SA1111_MSE,
167 .skpcr_mask = SKPCR_PMCLKEN,
168 .devid = SA1111_DEVID_PS2,
169 .irq = {
170 IRQ_MSRXINT,
171 IRQ_MSTXINT
172 },
173 },
174 {
175 .offset = 0x1800,
176 .skpcr_mask = 0,
177 .devid = SA1111_DEVID_PCMCIA,
178 .irq = {
179 IRQ_S0_READY_NINT,
180 IRQ_S0_CD_VALID,
181 IRQ_S0_BVD1_STSCHG,
182 IRQ_S1_READY_NINT,
183 IRQ_S1_CD_VALID,
184 IRQ_S1_BVD1_STSCHG,
185 },
186 },
187};
188
189/*
190 * SA1111 interrupt support. Since clearing an IRQ while there are
191 * active IRQs causes the interrupt output to pulse, the upper levels
192 * will call us again if there are more interrupts to process.
193 */
194static void
Russell King10dd5ce2006-11-23 11:41:32 +0000195sa1111_irq_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 unsigned int stat0, stat1, i;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100198 struct sa1111 *sachip = irq_get_handler_data(irq);
Eric Miao19851c52009-12-26 16:23:02 +0800199 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Eric Miao19851c52009-12-26 16:23:02 +0800201 stat0 = sa1111_readl(mapbase + SA1111_INTSTATCLR0);
202 stat1 = sa1111_readl(mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Eric Miao19851c52009-12-26 16:23:02 +0800204 sa1111_writel(stat0, mapbase + SA1111_INTSTATCLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100206 desc->irq_data.chip->irq_ack(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Eric Miao19851c52009-12-26 16:23:02 +0800208 sa1111_writel(stat1, mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (stat0 == 0 && stat1 == 0) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700211 do_bad_IRQ(irq, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return;
213 }
214
Eric Miao19851c52009-12-26 16:23:02 +0800215 for (i = 0; stat0; i++, stat0 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (stat0 & 1)
Eric Miao19851c52009-12-26 16:23:02 +0800217 generic_handle_irq(i + sachip->irq_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Eric Miao19851c52009-12-26 16:23:02 +0800219 for (i = 32; stat1; i++, stat1 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (stat1 & 1)
Eric Miao19851c52009-12-26 16:23:02 +0800221 generic_handle_irq(i + sachip->irq_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* For level-based interrupts */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100224 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Eric Miao19851c52009-12-26 16:23:02 +0800227#define SA1111_IRQMASK_LO(x) (1 << (x - sachip->irq_base))
228#define SA1111_IRQMASK_HI(x) (1 << (x - sachip->irq_base - 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100230static void sa1111_ack_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232}
233
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100234static void sa1111_mask_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100236 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800237 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned long ie0;
239
240 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100241 ie0 &= ~SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 writel(ie0, mapbase + SA1111_INTEN0);
243}
244
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100245static void sa1111_unmask_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100247 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800248 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned long ie0;
250
251 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100252 ie0 |= SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sa1111_writel(ie0, mapbase + SA1111_INTEN0);
254}
255
256/*
257 * Attempt to re-trigger the interrupt. The SA1111 contains a register
258 * (INTSET) which claims to do this. However, in practice no amount of
259 * manipulation of INTEN and INTSET guarantees that the interrupt will
260 * be triggered. In fact, its very difficult, if not impossible to get
261 * INTSET to re-trigger the interrupt.
262 */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100263static int sa1111_retrigger_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100265 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800266 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100267 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 unsigned long ip0;
269 int i;
270
271 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
272 for (i = 0; i < 8; i++) {
273 sa1111_writel(ip0 ^ mask, mapbase + SA1111_INTPOL0);
274 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
Pavel Machekcae39982011-01-10 16:35:34 -0600275 if (sa1111_readl(mapbase + SA1111_INTSTATCLR0) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 }
278
279 if (i == 8)
280 printk(KERN_ERR "Danger Will Robinson: failed to "
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100281 "re-trigger IRQ%d\n", d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return i == 8 ? -1 : 0;
283}
284
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100285static int sa1111_type_lowirq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100287 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800288 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100289 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned long ip0;
291
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100292 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100295 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return -EINVAL;
297
298 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100299 if (flags & IRQ_TYPE_EDGE_RISING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ip0 &= ~mask;
301 else
302 ip0 |= mask;
303 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
304 sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);
305
306 return 0;
307}
308
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100309static int sa1111_wake_lowirq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100311 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800312 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100313 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned long we0;
315
316 we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
317 if (on)
318 we0 |= mask;
319 else
320 we0 &= ~mask;
321 sa1111_writel(we0, mapbase + SA1111_WAKEEN0);
322
323 return 0;
324}
325
David Brownell38c677c2006-08-01 22:26:25 +0100326static struct irq_chip sa1111_low_chip = {
327 .name = "SA1111-l",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100328 .irq_ack = sa1111_ack_irq,
329 .irq_mask = sa1111_mask_lowirq,
330 .irq_unmask = sa1111_unmask_lowirq,
331 .irq_retrigger = sa1111_retrigger_lowirq,
332 .irq_set_type = sa1111_type_lowirq,
333 .irq_set_wake = sa1111_wake_lowirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100336static void sa1111_mask_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100338 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800339 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long ie1;
341
342 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100343 ie1 &= ~SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
345}
346
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100347static void sa1111_unmask_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100349 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800350 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unsigned long ie1;
352
353 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100354 ie1 |= SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
356}
357
358/*
359 * Attempt to re-trigger the interrupt. The SA1111 contains a register
360 * (INTSET) which claims to do this. However, in practice no amount of
361 * manipulation of INTEN and INTSET guarantees that the interrupt will
362 * be triggered. In fact, its very difficult, if not impossible to get
363 * INTSET to re-trigger the interrupt.
364 */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100365static int sa1111_retrigger_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100367 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800368 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100369 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long ip1;
371 int i;
372
373 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
374 for (i = 0; i < 8; i++) {
375 sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
376 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
377 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
378 break;
379 }
380
381 if (i == 8)
382 printk(KERN_ERR "Danger Will Robinson: failed to "
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100383 "re-trigger IRQ%d\n", d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return i == 8 ? -1 : 0;
385}
386
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100387static int sa1111_type_highirq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100389 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800390 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100391 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 unsigned long ip1;
393
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100394 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100397 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EINVAL;
399
400 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100401 if (flags & IRQ_TYPE_EDGE_RISING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ip1 &= ~mask;
403 else
404 ip1 |= mask;
405 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
406 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
407
408 return 0;
409}
410
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100411static int sa1111_wake_highirq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100413 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800414 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100415 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long we1;
417
418 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
419 if (on)
420 we1 |= mask;
421 else
422 we1 &= ~mask;
423 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
424
425 return 0;
426}
427
David Brownell38c677c2006-08-01 22:26:25 +0100428static struct irq_chip sa1111_high_chip = {
429 .name = "SA1111-h",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100430 .irq_ack = sa1111_ack_irq,
431 .irq_mask = sa1111_mask_highirq,
432 .irq_unmask = sa1111_unmask_highirq,
433 .irq_retrigger = sa1111_retrigger_highirq,
434 .irq_set_type = sa1111_type_highirq,
435 .irq_set_wake = sa1111_wake_highirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
437
438static void sa1111_setup_irq(struct sa1111 *sachip)
439{
440 void __iomem *irqbase = sachip->base + SA1111_INTC;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000441 unsigned i, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /*
444 * We're guaranteed that this region hasn't been taken.
445 */
446 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
447
448 /* disable all IRQs */
449 sa1111_writel(0, irqbase + SA1111_INTEN0);
450 sa1111_writel(0, irqbase + SA1111_INTEN1);
451 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
452 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
453
454 /*
455 * detect on rising edge. Note: Feb 2001 Errata for SA1111
456 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
457 */
458 sa1111_writel(0, irqbase + SA1111_INTPOL0);
459 sa1111_writel(SA1111_IRQMASK_HI(IRQ_S0_READY_NINT) |
460 SA1111_IRQMASK_HI(IRQ_S1_READY_NINT),
461 irqbase + SA1111_INTPOL1);
462
463 /* clear all IRQs */
464 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
465 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);
466
Russell Kingf03ecaa2012-01-16 00:09:22 +0000467 for (i = IRQ_GPAIN0; i <= SSPROR; i++) {
468 irq = sachip->irq_base + i;
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100469 irq_set_chip_and_handler(irq, &sa1111_low_chip,
470 handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100471 irq_set_chip_data(irq, sachip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
473 }
474
Russell Kingf03ecaa2012-01-16 00:09:22 +0000475 for (i = AUDXMTDMADONEA; i <= IRQ_S1_BVD1_STSCHG; i++) {
476 irq = sachip->irq_base + i;
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100477 irq_set_chip_and_handler(irq, &sa1111_high_chip,
478 handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100479 irq_set_chip_data(irq, sachip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
481 }
482
483 /*
484 * Register SA1111 interrupt
485 */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100486 irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
487 irq_set_handler_data(sachip->irq, sachip);
488 irq_set_chained_handler(sachip->irq, sa1111_irq_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/*
492 * Bring the SA1111 out of reset. This requires a set procedure:
493 * 1. nRESET asserted (by hardware)
494 * 2. CLK turned on from SA1110
495 * 3. nRESET deasserted
496 * 4. VCO turned on, PLL_BYPASS turned off
497 * 5. Wait lock time, then assert RCLKEn
498 * 7. PCR set to allow clocking of individual functions
499 *
500 * Until we've done this, the only registers we can access are:
501 * SBI_SKCR
502 * SBI_SMCR
503 * SBI_SKID
504 */
505static void sa1111_wake(struct sa1111 *sachip)
506{
507 unsigned long flags, r;
508
509 spin_lock_irqsave(&sachip->lock, flags);
510
Russell King97d654f2006-03-15 15:54:37 +0000511 clk_enable(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * Turn VCO on, and disable PLL Bypass.
515 */
516 r = sa1111_readl(sachip->base + SA1111_SKCR);
517 r &= ~SKCR_VCO_OFF;
518 sa1111_writel(r, sachip->base + SA1111_SKCR);
519 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
520 sa1111_writel(r, sachip->base + SA1111_SKCR);
521
522 /*
523 * Wait lock time. SA1111 manual _doesn't_
524 * specify a figure for this! We choose 100us.
525 */
526 udelay(100);
527
528 /*
529 * Enable RCLK. We also ensure that RDYEN is set.
530 */
531 r |= SKCR_RCLKEN | SKCR_RDYEN;
532 sa1111_writel(r, sachip->base + SA1111_SKCR);
533
534 /*
535 * Wait 14 RCLK cycles for the chip to finish coming out
536 * of reset. (RCLK=24MHz). This is 590ns.
537 */
538 udelay(1);
539
540 /*
541 * Ensure all clocks are initially off.
542 */
543 sa1111_writel(0, sachip->base + SA1111_SKPCR);
544
545 spin_unlock_irqrestore(&sachip->lock, flags);
546}
547
548#ifdef CONFIG_ARCH_SA1100
549
550static u32 sa1111_dma_mask[] = {
551 ~0,
552 ~(1 << 20),
553 ~(1 << 23),
554 ~(1 << 24),
555 ~(1 << 25),
556 ~(1 << 20),
557 ~(1 << 20),
558 0,
559};
560
561/*
562 * Configure the SA1111 shared memory controller.
563 */
564void
565sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
566 unsigned int cas_latency)
567{
568 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
569
570 if (cas_latency == 3)
571 smcr |= SMCR_CLAT;
572
573 sa1111_writel(smcr, sachip->base + SA1111_SMCR);
574
575 /*
576 * Now clear the bits in the DMA mask to work around the SA1111
577 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
578 * Chip Specification Update, June 2000, Erratum #7).
579 */
580 if (sachip->dev->dma_mask)
581 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
582
583 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
584}
Russell King0703ed22011-07-04 08:32:21 +0100585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Russell King0703ed22011-07-04 08:32:21 +0100587#ifdef CONFIG_DMABOUNCE
588/*
589 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
590 * Chip Specification Update" (June 2000), erratum #7, there is a
591 * significant bug in the SA1111 SDRAM shared memory controller. If
592 * an access to a region of memory above 1MB relative to the bank base,
593 * it is important that address bit 10 _NOT_ be asserted. Depending
594 * on the configuration of the RAM, bit 10 may correspond to one
595 * of several different (processor-relative) address bits.
596 *
597 * This routine only identifies whether or not a given DMA address
598 * is susceptible to the bug.
599 *
600 * This should only get called for sa1111_device types due to the
601 * way we configure our device dma_masks.
602 */
603static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
604{
605 /*
606 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
607 * User's Guide" mentions that jumpers R51 and R52 control the
608 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
609 * SDRAM bank 1 on Neponset). The default configuration selects
610 * Assabet, so any address in bank 1 is necessarily invalid.
611 */
612 return (machine_is_assabet() || machine_is_pfs168()) &&
613 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615#endif
616
617static void sa1111_dev_release(struct device *_dev)
618{
619 struct sa1111_dev *dev = SA1111_DEV(_dev);
620
621 release_resource(&dev->res);
622 kfree(dev);
623}
624
625static int
626sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
627 struct sa1111_dev_info *info)
628{
629 struct sa1111_dev *dev;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000630 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int ret;
632
Russell Kingd2a02b92006-03-20 19:46:41 +0000633 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!dev) {
635 ret = -ENOMEM;
636 goto out;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Kay Sievers3f978702008-05-30 17:42:11 +0200639 dev_set_name(&dev->dev, "%4.4lx", info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 dev->devid = info->devid;
641 dev->dev.parent = sachip->dev;
642 dev->dev.bus = &sa1111_bus_type;
643 dev->dev.release = sa1111_dev_release;
644 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
645 dev->res.start = sachip->phys + info->offset;
646 dev->res.end = dev->res.start + 511;
Kay Sievers3f978702008-05-30 17:42:11 +0200647 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 dev->res.flags = IORESOURCE_MEM;
649 dev->mapbase = sachip->base + info->offset;
650 dev->skpcr_mask = info->skpcr_mask;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000651
652 for (i = 0; i < ARRAY_SIZE(info->irq); i++)
653 dev->irq[i] = sachip->irq_base + info->irq[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 ret = request_resource(parent, &dev->res);
656 if (ret) {
657 printk("SA1111: failed to allocate resource for %s\n",
658 dev->res.name);
Kay Sievers3f978702008-05-30 17:42:11 +0200659 dev_set_name(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 kfree(dev);
661 goto out;
662 }
663
664
665 ret = device_register(&dev->dev);
666 if (ret) {
667 release_resource(&dev->res);
668 kfree(dev);
669 goto out;
670 }
671
Russell King957cf332008-10-17 13:39:26 +0100672#ifdef CONFIG_DMABOUNCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * If the parent device has a DMA mask associated with it,
675 * propagate it down to the children.
676 */
677 if (sachip->dev->dma_mask) {
678 dev->dma_mask = *sachip->dev->dma_mask;
679 dev->dev.dma_mask = &dev->dma_mask;
680
681 if (dev->dma_mask != 0xffffffffUL) {
Russell King0703ed22011-07-04 08:32:21 +0100682 ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
683 sa1111_needs_bounce);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ret) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200685 dev_err(&dev->dev, "SA1111: Failed to register"
686 " with dmabounce\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 device_unregister(&dev->dev);
688 }
689 }
690 }
Russell King957cf332008-10-17 13:39:26 +0100691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693out:
694 return ret;
695}
696
697/**
698 * sa1111_probe - probe for a single SA1111 chip.
699 * @phys_addr: physical address of device.
700 *
701 * Probe for a SA1111 chip. This must be called
702 * before any other SA1111-specific code.
703 *
704 * Returns:
705 * %-ENODEV device not found.
706 * %-EBUSY physical address already marked in-use.
Russell Kingf03ecaa2012-01-16 00:09:22 +0000707 * %-EINVAL no platform data passed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * %0 successful.
709 */
Uwe Kleine-König055d1962010-09-02 09:14:21 +0100710static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711__sa1111_probe(struct device *me, struct resource *mem, int irq)
712{
Russell Kingf03ecaa2012-01-16 00:09:22 +0000713 struct sa1111_platform_data *pd = me->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct sa1111 *sachip;
715 unsigned long id;
David Brownell416112f2006-08-27 13:09:14 +0100716 unsigned int has_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int i, ret = -ENODEV;
718
Russell Kingf03ecaa2012-01-16 00:09:22 +0000719 if (!pd)
720 return -EINVAL;
721
Russell Kingd2a02b92006-03-20 19:46:41 +0000722 sachip = kzalloc(sizeof(struct sa1111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!sachip)
724 return -ENOMEM;
725
Ian Molton13f75582008-07-08 10:32:50 +0100726 sachip->clk = clk_get(me, "SA1111_CLK");
Julia Lawall442a9022008-12-13 16:37:59 +0100727 if (IS_ERR(sachip->clk)) {
Russell King97d654f2006-03-15 15:54:37 +0000728 ret = PTR_ERR(sachip->clk);
729 goto err_free;
730 }
731
Russell King72ae00c2011-09-22 11:41:07 +0100732 ret = clk_prepare(sachip->clk);
733 if (ret)
734 goto err_clkput;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 spin_lock_init(&sachip->lock);
737
738 sachip->dev = me;
739 dev_set_drvdata(sachip->dev, sachip);
740
741 sachip->phys = mem->start;
742 sachip->irq = irq;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000743 sachip->irq_base = pd->irq_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /*
746 * Map the whole region. This also maps the
747 * registers for our children.
748 */
749 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
750 if (!sachip->base) {
751 ret = -ENOMEM;
Russell King72ae00c2011-09-22 11:41:07 +0100752 goto err_clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 /*
756 * Probe for the chip. Only touch the SBI registers.
757 */
758 id = sa1111_readl(sachip->base + SA1111_SKID);
759 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
760 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
761 ret = -ENODEV;
Russell King97d654f2006-03-15 15:54:37 +0000762 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 printk(KERN_INFO "SA1111 Microprocessor Companion Chip: "
766 "silicon revision %lx, metal revision %lx\n",
767 (id & SKID_SIREV_MASK)>>4, (id & SKID_MTREV_MASK));
768
769 /*
770 * We found it. Wake the chip up, and initialise.
771 */
772 sa1111_wake(sachip);
773
774#ifdef CONFIG_ARCH_SA1100
David Brownell416112f2006-08-27 13:09:14 +0100775 {
776 unsigned int val;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * The SDRAM configuration of the SA1110 and the SA1111 must
780 * match. This is very important to ensure that SA1111 accesses
781 * don't corrupt the SDRAM. Note that this ungates the SA1111's
782 * MBGNT signal, so we must have called sa1110_mb_disable()
783 * beforehand.
784 */
785 sa1111_configure_smc(sachip, 1,
786 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
787 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
788
789 /*
790 * We only need to turn on DCLK whenever we want to use the
791 * DMA. It can otherwise be held firmly in the off position.
792 * (currently, we always enable it.)
793 */
794 val = sa1111_readl(sachip->base + SA1111_SKPCR);
795 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
796
797 /*
798 * Enable the SA1110 memory bus request and grant signals.
799 */
800 sa1110_mb_enable();
David Brownell416112f2006-08-27 13:09:14 +0100801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#endif
803
804 /*
805 * The interrupt controller must be initialised before any
806 * other device to ensure that the interrupts are available.
807 */
808 if (sachip->irq != NO_IRQ)
809 sa1111_setup_irq(sachip);
810
811 g_sa1111 = sachip;
812
813 has_devs = ~0;
814 if (machine_is_assabet() || machine_is_jornada720() ||
815 machine_is_badge4())
816 has_devs &= ~(1 << 4);
817 else
818 has_devs &= ~(1 << 1);
819
820 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
821 if (has_devs & (1 << i))
822 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
823
824 return 0;
825
Russell King97d654f2006-03-15 15:54:37 +0000826 err_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 iounmap(sachip->base);
Russell King72ae00c2011-09-22 11:41:07 +0100828 err_clk_unprep:
829 clk_unprepare(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000830 err_clkput:
831 clk_put(sachip->clk);
832 err_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 kfree(sachip);
834 return ret;
835}
836
Russell King522c37b2005-06-22 09:52:26 +0100837static int sa1111_remove_one(struct device *dev, void *data)
838{
839 device_unregister(dev);
840 return 0;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static void __sa1111_remove(struct sa1111 *sachip)
844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 void __iomem *irqbase = sachip->base + SA1111_INTC;
846
Russell King522c37b2005-06-22 09:52:26 +0100847 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* disable all IRQs */
850 sa1111_writel(0, irqbase + SA1111_INTEN0);
851 sa1111_writel(0, irqbase + SA1111_INTEN1);
852 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
853 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
854
Russell King97d654f2006-03-15 15:54:37 +0000855 clk_disable(sachip->clk);
Russell King72ae00c2011-09-22 11:41:07 +0100856 clk_unprepare(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (sachip->irq != NO_IRQ) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100859 irq_set_chained_handler(sachip->irq, NULL);
860 irq_set_handler_data(sachip->irq, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 release_mem_region(sachip->phys + SA1111_INTC, 512);
863 }
864
865 iounmap(sachip->base);
Russell King97d654f2006-03-15 15:54:37 +0000866 clk_put(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 kfree(sachip);
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870struct sa1111_save_data {
871 unsigned int skcr;
872 unsigned int skpcr;
873 unsigned int skcdr;
874 unsigned char skaud;
875 unsigned char skpwm0;
876 unsigned char skpwm1;
877
878 /*
879 * Interrupt controller
880 */
881 unsigned int intpol0;
882 unsigned int intpol1;
883 unsigned int inten0;
884 unsigned int inten1;
885 unsigned int wakepol0;
886 unsigned int wakepol1;
887 unsigned int wakeen0;
888 unsigned int wakeen1;
889};
890
891#ifdef CONFIG_PM
892
Russell King3ae5eae2005-11-09 22:32:44 +0000893static int sa1111_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Russell King3ae5eae2005-11-09 22:32:44 +0000895 struct sa1111 *sachip = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct sa1111_save_data *save;
897 unsigned long flags;
898 unsigned int val;
899 void __iomem *base;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
902 if (!save)
903 return -ENOMEM;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700904 sachip->saved_state = save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 spin_lock_irqsave(&sachip->lock, flags);
907
908 /*
909 * Save state.
910 */
911 base = sachip->base;
912 save->skcr = sa1111_readl(base + SA1111_SKCR);
913 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
914 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
915 save->skaud = sa1111_readl(base + SA1111_SKAUD);
916 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
917 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
918
Russell Kinga22db0f32012-02-03 19:05:13 +0000919 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
920 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 base = sachip->base + SA1111_INTC;
923 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
924 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
925 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
926 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
927 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
928 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
929 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
930 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
931
932 /*
933 * Disable.
934 */
935 val = sa1111_readl(sachip->base + SA1111_SKCR);
936 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Russell King97d654f2006-03-15 15:54:37 +0000938 clk_disable(sachip->clk);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_unlock_irqrestore(&sachip->lock, flags);
941
Russell King29c140b2012-02-03 19:03:31 +0000942#ifdef CONFIG_ARCH_SA1100
943 sa1110_mb_disable();
944#endif
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947}
948
949/*
950 * sa1111_resume - Restore the SA1111 device state.
951 * @dev: device to restore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
953 * Restore the general state of the SA1111; clock control and
954 * interrupt controller. Other parts of the SA1111 must be
955 * restored by their respective drivers, and must be called
956 * via LDM after this function.
957 */
Russell King3ae5eae2005-11-09 22:32:44 +0000958static int sa1111_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Russell King3ae5eae2005-11-09 22:32:44 +0000960 struct sa1111 *sachip = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct sa1111_save_data *save;
962 unsigned long flags, id;
963 void __iomem *base;
964
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700965 save = sachip->saved_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!save)
967 return 0;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
970 * Ensure that the SA1111 is still here.
971 * FIXME: shouldn't do this here.
972 */
973 id = sa1111_readl(sachip->base + SA1111_SKID);
974 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
975 __sa1111_remove(sachip);
Russell King3ae5eae2005-11-09 22:32:44 +0000976 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 kfree(save);
978 return 0;
979 }
980
981 /*
982 * First of all, wake up the chip.
983 */
984 sa1111_wake(sachip);
Marek Vašut3defb242010-05-26 23:53:09 +0100985
Russell King29c140b2012-02-03 19:03:31 +0000986#ifdef CONFIG_ARCH_SA1100
987 /* Enable the memory bus request/grant signals */
988 sa1110_mb_enable();
989#endif
990
Marek Vašut3defb242010-05-26 23:53:09 +0100991 /*
992 * Only lock for write ops. Also, sa1111_wake must be called with
993 * released spinlock!
994 */
995 spin_lock_irqsave(&sachip->lock, flags);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
998 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
999
1000 base = sachip->base;
1001 sa1111_writel(save->skcr, base + SA1111_SKCR);
1002 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
1003 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
1004 sa1111_writel(save->skaud, base + SA1111_SKAUD);
1005 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
1006 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
1007
1008 base = sachip->base + SA1111_INTC;
1009 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
1010 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
1011 sa1111_writel(save->inten0, base + SA1111_INTEN0);
1012 sa1111_writel(save->inten1, base + SA1111_INTEN1);
1013 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
1014 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
1015 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
1016 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
1017
1018 spin_unlock_irqrestore(&sachip->lock, flags);
1019
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001020 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 kfree(save);
1022
1023 return 0;
1024}
1025
1026#else
1027#define sa1111_suspend NULL
1028#define sa1111_resume NULL
1029#endif
1030
Kristoffer Ericson5d438392008-10-21 19:47:22 +01001031static int __devinit sa1111_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct resource *mem;
1034 int irq;
1035
1036 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1037 if (!mem)
1038 return -EINVAL;
1039 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +00001040 if (irq < 0)
1041 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Russell King3ae5eae2005-11-09 22:32:44 +00001043 return __sa1111_probe(&pdev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
Russell King3ae5eae2005-11-09 22:32:44 +00001046static int sa1111_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Russell King3ae5eae2005-11-09 22:32:44 +00001048 struct sa1111 *sachip = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (sachip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051#ifdef CONFIG_PM
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001052 kfree(sachip->saved_state);
1053 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#endif
Julia Lawallf2d24202010-07-30 17:17:28 +02001055 __sa1111_remove(sachip);
1056 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058
1059 return 0;
1060}
1061
1062/*
1063 * Not sure if this should be on the system bus or not yet.
1064 * We really want some way to register a system device at
1065 * the per-machine level, and then have this driver pick
1066 * up the registered devices.
1067 *
1068 * We also need to handle the SDRAM configuration for
1069 * PXA250/SA1110 machine classes.
1070 */
Russell King3ae5eae2005-11-09 22:32:44 +00001071static struct platform_driver sa1111_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 .probe = sa1111_probe,
1073 .remove = sa1111_remove,
1074 .suspend = sa1111_suspend,
1075 .resume = sa1111_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001076 .driver = {
1077 .name = "sa1111",
Russell King4d5d1122012-01-14 16:09:22 +00001078 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001079 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080};
1081
1082/*
1083 * Get the parent device driver (us) structure
1084 * from a child function device
1085 */
1086static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1087{
1088 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1089}
1090
1091/*
1092 * The bits in the opdiv field are non-linear.
1093 */
1094static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1095
1096static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1097{
1098 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1099
1100 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1101
1102 fbdiv = (skcdr & 0x007f) + 2;
1103 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1104 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1105
1106 return 3686400 * fbdiv / (ipdiv * opdiv);
1107}
1108
1109/**
1110 * sa1111_pll_clock - return the current PLL clock frequency.
1111 * @sadev: SA1111 function block
1112 *
1113 * BUG: we should look at SKCR. We also blindly believe that
1114 * the chip is being fed with the 3.6864MHz clock.
1115 *
1116 * Returns the PLL clock in Hz.
1117 */
1118unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1119{
1120 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1121
1122 return __sa1111_pll_clock(sachip);
1123}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001124EXPORT_SYMBOL(sa1111_pll_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126/**
1127 * sa1111_select_audio_mode - select I2S or AC link mode
1128 * @sadev: SA1111 function block
1129 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1130 *
1131 * Frob the SKCR to select AC Link mode or I2S mode for
1132 * the audio block.
1133 */
1134void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1135{
1136 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1137 unsigned long flags;
1138 unsigned int val;
1139
1140 spin_lock_irqsave(&sachip->lock, flags);
1141
1142 val = sa1111_readl(sachip->base + SA1111_SKCR);
1143 if (mode == SA1111_AUDIO_I2S) {
1144 val &= ~SKCR_SELAC;
1145 } else {
1146 val |= SKCR_SELAC;
1147 }
1148 sa1111_writel(val, sachip->base + SA1111_SKCR);
1149
1150 spin_unlock_irqrestore(&sachip->lock, flags);
1151}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001152EXPORT_SYMBOL(sa1111_select_audio_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154/**
1155 * sa1111_set_audio_rate - set the audio sample rate
1156 * @sadev: SA1111 SAC function block
1157 * @rate: sample rate to select
1158 */
1159int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1160{
1161 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1162 unsigned int div;
1163
1164 if (sadev->devid != SA1111_DEVID_SAC)
1165 return -EINVAL;
1166
1167 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1168 if (div == 0)
1169 div = 1;
1170 if (div > 128)
1171 div = 128;
1172
1173 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1174
1175 return 0;
1176}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001177EXPORT_SYMBOL(sa1111_set_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179/**
1180 * sa1111_get_audio_rate - get the audio sample rate
1181 * @sadev: SA1111 SAC function block device
1182 */
1183int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1184{
1185 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1186 unsigned long div;
1187
1188 if (sadev->devid != SA1111_DEVID_SAC)
1189 return -EINVAL;
1190
1191 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1192
1193 return __sa1111_pll_clock(sachip) / (256 * div);
1194}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001195EXPORT_SYMBOL(sa1111_get_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197void sa1111_set_io_dir(struct sa1111_dev *sadev,
1198 unsigned int bits, unsigned int dir,
1199 unsigned int sleep_dir)
1200{
1201 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1202 unsigned long flags;
1203 unsigned int val;
1204 void __iomem *gpio = sachip->base + SA1111_GPIO;
1205
1206#define MODIFY_BITS(port, mask, dir) \
1207 if (mask) { \
1208 val = sa1111_readl(port); \
1209 val &= ~(mask); \
1210 val |= (dir) & (mask); \
1211 sa1111_writel(val, port); \
1212 }
1213
1214 spin_lock_irqsave(&sachip->lock, flags);
1215 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1216 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1217 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1218
1219 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1220 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1221 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1222 spin_unlock_irqrestore(&sachip->lock, flags);
1223}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001224EXPORT_SYMBOL(sa1111_set_io_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1227{
1228 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1229 unsigned long flags;
1230 unsigned int val;
1231 void __iomem *gpio = sachip->base + SA1111_GPIO;
1232
1233 spin_lock_irqsave(&sachip->lock, flags);
1234 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1235 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1236 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1237 spin_unlock_irqrestore(&sachip->lock, flags);
1238}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001239EXPORT_SYMBOL(sa1111_set_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1242{
1243 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1244 unsigned long flags;
1245 unsigned int val;
1246 void __iomem *gpio = sachip->base + SA1111_GPIO;
1247
1248 spin_lock_irqsave(&sachip->lock, flags);
1249 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1250 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1251 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1252 spin_unlock_irqrestore(&sachip->lock, flags);
1253}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001254EXPORT_SYMBOL(sa1111_set_sleep_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256/*
1257 * Individual device operations.
1258 */
1259
1260/**
1261 * sa1111_enable_device - enable an on-chip SA1111 function block
1262 * @sadev: SA1111 function block device to enable
1263 */
1264void sa1111_enable_device(struct sa1111_dev *sadev)
1265{
1266 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1267 unsigned long flags;
1268 unsigned int val;
1269
1270 spin_lock_irqsave(&sachip->lock, flags);
1271 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1272 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1273 spin_unlock_irqrestore(&sachip->lock, flags);
1274}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001275EXPORT_SYMBOL(sa1111_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/**
1278 * sa1111_disable_device - disable an on-chip SA1111 function block
1279 * @sadev: SA1111 function block device to disable
1280 */
1281void sa1111_disable_device(struct sa1111_dev *sadev)
1282{
1283 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1284 unsigned long flags;
1285 unsigned int val;
1286
1287 spin_lock_irqsave(&sachip->lock, flags);
1288 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1289 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1290 spin_unlock_irqrestore(&sachip->lock, flags);
1291}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001292EXPORT_SYMBOL(sa1111_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294/*
1295 * SA1111 "Register Access Bus."
1296 *
1297 * We model this as a regular bus type, and hang devices directly
1298 * off this.
1299 */
1300static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1301{
1302 struct sa1111_dev *dev = SA1111_DEV(_dev);
1303 struct sa1111_driver *drv = SA1111_DRV(_drv);
1304
1305 return dev->devid == drv->devid;
1306}
1307
1308static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
1309{
1310 struct sa1111_dev *sadev = SA1111_DEV(dev);
1311 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1312 int ret = 0;
1313
1314 if (drv && drv->suspend)
1315 ret = drv->suspend(sadev, state);
1316 return ret;
1317}
1318
1319static int sa1111_bus_resume(struct device *dev)
1320{
1321 struct sa1111_dev *sadev = SA1111_DEV(dev);
1322 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1323 int ret = 0;
1324
1325 if (drv && drv->resume)
1326 ret = drv->resume(sadev);
1327 return ret;
1328}
1329
1330static int sa1111_bus_probe(struct device *dev)
1331{
1332 struct sa1111_dev *sadev = SA1111_DEV(dev);
1333 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1334 int ret = -ENODEV;
1335
1336 if (drv->probe)
1337 ret = drv->probe(sadev);
1338 return ret;
1339}
1340
1341static int sa1111_bus_remove(struct device *dev)
1342{
1343 struct sa1111_dev *sadev = SA1111_DEV(dev);
1344 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1345 int ret = 0;
1346
1347 if (drv->remove)
1348 ret = drv->remove(sadev);
1349 return ret;
1350}
1351
1352struct bus_type sa1111_bus_type = {
1353 .name = "sa1111-rab",
1354 .match = sa1111_match,
Russell King2876ba42006-01-05 14:32:32 +00001355 .probe = sa1111_bus_probe,
1356 .remove = sa1111_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 .suspend = sa1111_bus_suspend,
1358 .resume = sa1111_bus_resume,
1359};
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001360EXPORT_SYMBOL(sa1111_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362int sa1111_driver_register(struct sa1111_driver *driver)
1363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 driver->drv.bus = &sa1111_bus_type;
1365 return driver_register(&driver->drv);
1366}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001367EXPORT_SYMBOL(sa1111_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369void sa1111_driver_unregister(struct sa1111_driver *driver)
1370{
1371 driver_unregister(&driver->drv);
1372}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001373EXPORT_SYMBOL(sa1111_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375static int __init sa1111_init(void)
1376{
1377 int ret = bus_register(&sa1111_bus_type);
1378 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001379 platform_driver_register(&sa1111_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return ret;
1381}
1382
1383static void __exit sa1111_exit(void)
1384{
Russell King3ae5eae2005-11-09 22:32:44 +00001385 platform_driver_unregister(&sa1111_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 bus_unregister(&sa1111_bus_type);
1387}
1388
Russell King72724382005-11-15 19:04:22 +00001389subsys_initcall(sa1111_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390module_exit(sa1111_exit);
1391
1392MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1393MODULE_LICENSE("GPL");