blob: 16cc3b5122ad6f06751cef464327058fe8ba4679 [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>
Russell King17cf5012016-08-31 08:49:45 +010018#include <linux/gpio/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Russell King36d31212012-01-24 21:25:20 +000020#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010025#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/dma-mapping.h>
Russell King97d654f2006-03-15 15:54:37 +000029#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010030#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/mach/irq.h>
Russell King36d31212012-01-24 21:25:20 +000034#include <asm/mach-types.h>
Russell King45e109d2005-11-16 18:29:51 +000035#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/hardware/sa1111.h>
38
Eric Miao19851c52009-12-26 16:23:02 +080039/* SA1111 IRQs */
40#define IRQ_GPAIN0 (0)
41#define IRQ_GPAIN1 (1)
42#define IRQ_GPAIN2 (2)
43#define IRQ_GPAIN3 (3)
44#define IRQ_GPBIN0 (4)
45#define IRQ_GPBIN1 (5)
46#define IRQ_GPBIN2 (6)
47#define IRQ_GPBIN3 (7)
48#define IRQ_GPBIN4 (8)
49#define IRQ_GPBIN5 (9)
50#define IRQ_GPCIN0 (10)
51#define IRQ_GPCIN1 (11)
52#define IRQ_GPCIN2 (12)
53#define IRQ_GPCIN3 (13)
54#define IRQ_GPCIN4 (14)
55#define IRQ_GPCIN5 (15)
56#define IRQ_GPCIN6 (16)
57#define IRQ_GPCIN7 (17)
58#define IRQ_MSTXINT (18)
59#define IRQ_MSRXINT (19)
60#define IRQ_MSSTOPERRINT (20)
61#define IRQ_TPTXINT (21)
62#define IRQ_TPRXINT (22)
63#define IRQ_TPSTOPERRINT (23)
64#define SSPXMTINT (24)
65#define SSPRCVINT (25)
66#define SSPROR (26)
67#define AUDXMTDMADONEA (32)
68#define AUDRCVDMADONEA (33)
69#define AUDXMTDMADONEB (34)
70#define AUDRCVDMADONEB (35)
71#define AUDTFSR (36)
72#define AUDRFSR (37)
73#define AUDTUR (38)
74#define AUDROR (39)
75#define AUDDTS (40)
76#define AUDRDD (41)
77#define AUDSTO (42)
78#define IRQ_USBPWR (43)
79#define IRQ_HCIM (44)
80#define IRQ_HCIBUFFACC (45)
81#define IRQ_HCIRMTWKP (46)
82#define IRQ_NHCIMFCIR (47)
83#define IRQ_USB_PORT_RESUME (48)
84#define IRQ_S0_READY_NINT (49)
85#define IRQ_S1_READY_NINT (50)
86#define IRQ_S0_CD_VALID (51)
87#define IRQ_S1_CD_VALID (52)
88#define IRQ_S0_BVD1_STSCHG (53)
89#define IRQ_S1_BVD1_STSCHG (54)
Russell King36d31212012-01-24 21:25:20 +000090#define SA1111_IRQ_NR (55)
Eric Miao19851c52009-12-26 16:23:02 +080091
Russell King29c140b2012-02-03 19:03:31 +000092extern void sa1110_mb_enable(void);
93extern void sa1110_mb_disable(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/*
96 * We keep the following data for the overall SA1111. Note that the
97 * struct device and struct resource are "fake"; they should be supplied
98 * by the bus above us. However, in the interests of getting all SA1111
99 * drivers converted over to the device model, we provide this as an
100 * anchor point for all the other drivers.
101 */
102struct sa1111 {
103 struct device *dev;
Russell King97d654f2006-03-15 15:54:37 +0000104 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned long phys;
106 int irq;
Eric Miao19851c52009-12-26 16:23:02 +0800107 int irq_base; /* base for cascaded on-chip IRQs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 spinlock_t lock;
109 void __iomem *base;
Russell Kingae99ddb2012-01-26 13:25:47 +0000110 struct sa1111_platform_data *pdata;
Russell King17cf5012016-08-31 08:49:45 +0100111 struct gpio_chip gc;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700112#ifdef CONFIG_PM
113 void *saved_state;
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/*
118 * We _really_ need to eliminate this. Its only users
119 * are the PWM and DMA checking code.
120 */
121static struct sa1111 *g_sa1111;
122
123struct sa1111_dev_info {
124 unsigned long offset;
125 unsigned long skpcr_mask;
Russell King21d1c772012-01-29 10:22:49 +0000126 bool dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned int devid;
128 unsigned int irq[6];
129};
130
131static struct sa1111_dev_info sa1111_devices[] = {
132 {
133 .offset = SA1111_USB,
134 .skpcr_mask = SKPCR_UCLKEN,
Russell King21d1c772012-01-29 10:22:49 +0000135 .dma = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .devid = SA1111_DEVID_USB,
137 .irq = {
138 IRQ_USBPWR,
139 IRQ_HCIM,
140 IRQ_HCIBUFFACC,
141 IRQ_HCIRMTWKP,
142 IRQ_NHCIMFCIR,
143 IRQ_USB_PORT_RESUME
144 },
145 },
146 {
147 .offset = 0x0600,
148 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
Russell King21d1c772012-01-29 10:22:49 +0000149 .dma = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 .devid = SA1111_DEVID_SAC,
151 .irq = {
152 AUDXMTDMADONEA,
153 AUDXMTDMADONEB,
154 AUDRCVDMADONEA,
155 AUDRCVDMADONEB
156 },
157 },
158 {
159 .offset = 0x0800,
160 .skpcr_mask = SKPCR_SCLKEN,
161 .devid = SA1111_DEVID_SSP,
162 },
163 {
164 .offset = SA1111_KBD,
165 .skpcr_mask = SKPCR_PTCLKEN,
Russell Kinge5c0fc42012-01-25 10:42:52 +0000166 .devid = SA1111_DEVID_PS2_KBD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .irq = {
168 IRQ_TPRXINT,
169 IRQ_TPTXINT
170 },
171 },
172 {
173 .offset = SA1111_MSE,
174 .skpcr_mask = SKPCR_PMCLKEN,
Russell Kinge5c0fc42012-01-25 10:42:52 +0000175 .devid = SA1111_DEVID_PS2_MSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .irq = {
177 IRQ_MSRXINT,
178 IRQ_MSTXINT
179 },
180 },
181 {
182 .offset = 0x1800,
183 .skpcr_mask = 0,
184 .devid = SA1111_DEVID_PCMCIA,
185 .irq = {
186 IRQ_S0_READY_NINT,
187 IRQ_S0_CD_VALID,
188 IRQ_S0_BVD1_STSCHG,
189 IRQ_S1_READY_NINT,
190 IRQ_S1_CD_VALID,
191 IRQ_S1_BVD1_STSCHG,
192 },
193 },
194};
195
196/*
197 * SA1111 interrupt support. Since clearing an IRQ while there are
198 * active IRQs causes the interrupt output to pulse, the upper levels
199 * will call us again if there are more interrupts to process.
200 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200201static void sa1111_irq_handler(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 unsigned int stat0, stat1, i;
Jiang Liuf5753982015-06-04 12:13:19 +0800204 struct sa1111 *sachip = irq_desc_get_handler_data(desc);
Eric Miao19851c52009-12-26 16:23:02 +0800205 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Eric Miao19851c52009-12-26 16:23:02 +0800207 stat0 = sa1111_readl(mapbase + SA1111_INTSTATCLR0);
208 stat1 = sa1111_readl(mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Eric Miao19851c52009-12-26 16:23:02 +0800210 sa1111_writel(stat0, mapbase + SA1111_INTSTATCLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100212 desc->irq_data.chip->irq_ack(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Eric Miao19851c52009-12-26 16:23:02 +0800214 sa1111_writel(stat1, mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (stat0 == 0 && stat1 == 0) {
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200217 do_bad_IRQ(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return;
219 }
220
Eric Miao19851c52009-12-26 16:23:02 +0800221 for (i = 0; stat0; i++, stat0 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (stat0 & 1)
Eric Miao19851c52009-12-26 16:23:02 +0800223 generic_handle_irq(i + sachip->irq_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Eric Miao19851c52009-12-26 16:23:02 +0800225 for (i = 32; stat1; i++, stat1 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (stat1 & 1)
Eric Miao19851c52009-12-26 16:23:02 +0800227 generic_handle_irq(i + sachip->irq_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* For level-based interrupts */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100230 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Eric Miao19851c52009-12-26 16:23:02 +0800233#define SA1111_IRQMASK_LO(x) (1 << (x - sachip->irq_base))
234#define SA1111_IRQMASK_HI(x) (1 << (x - sachip->irq_base - 32))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100236static void sa1111_ack_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238}
239
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100240static void sa1111_mask_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100242 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800243 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned long ie0;
245
246 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100247 ie0 &= ~SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 writel(ie0, mapbase + SA1111_INTEN0);
249}
250
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100251static void sa1111_unmask_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100253 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800254 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned long ie0;
256
257 ie0 = sa1111_readl(mapbase + SA1111_INTEN0);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100258 ie0 |= SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sa1111_writel(ie0, mapbase + SA1111_INTEN0);
260}
261
262/*
263 * Attempt to re-trigger the interrupt. The SA1111 contains a register
264 * (INTSET) which claims to do this. However, in practice no amount of
265 * manipulation of INTEN and INTSET guarantees that the interrupt will
266 * be triggered. In fact, its very difficult, if not impossible to get
267 * INTSET to re-trigger the interrupt.
268 */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100269static int sa1111_retrigger_lowirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100271 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800272 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100273 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned long ip0;
275 int i;
276
277 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
278 for (i = 0; i < 8; i++) {
279 sa1111_writel(ip0 ^ mask, mapbase + SA1111_INTPOL0);
280 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
Pavel Machekcae39982011-01-10 16:35:34 -0600281 if (sa1111_readl(mapbase + SA1111_INTSTATCLR0) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 }
284
285 if (i == 8)
Russell King4ed89f22014-10-28 11:26:42 +0000286 pr_err("Danger Will Robinson: failed to re-trigger IRQ%d\n",
287 d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return i == 8 ? -1 : 0;
289}
290
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100291static int sa1111_type_lowirq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100293 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800294 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100295 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned long ip0;
297
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100298 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100301 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EINVAL;
303
304 ip0 = sa1111_readl(mapbase + SA1111_INTPOL0);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100305 if (flags & IRQ_TYPE_EDGE_RISING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ip0 &= ~mask;
307 else
308 ip0 |= mask;
309 sa1111_writel(ip0, mapbase + SA1111_INTPOL0);
310 sa1111_writel(ip0, mapbase + SA1111_WAKEPOL0);
311
312 return 0;
313}
314
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100315static int sa1111_wake_lowirq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100317 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800318 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100319 unsigned int mask = SA1111_IRQMASK_LO(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned long we0;
321
322 we0 = sa1111_readl(mapbase + SA1111_WAKEEN0);
323 if (on)
324 we0 |= mask;
325 else
326 we0 &= ~mask;
327 sa1111_writel(we0, mapbase + SA1111_WAKEEN0);
328
329 return 0;
330}
331
David Brownell38c677c2006-08-01 22:26:25 +0100332static struct irq_chip sa1111_low_chip = {
333 .name = "SA1111-l",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100334 .irq_ack = sa1111_ack_irq,
335 .irq_mask = sa1111_mask_lowirq,
336 .irq_unmask = sa1111_unmask_lowirq,
337 .irq_retrigger = sa1111_retrigger_lowirq,
338 .irq_set_type = sa1111_type_lowirq,
339 .irq_set_wake = sa1111_wake_lowirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100342static void sa1111_mask_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100344 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800345 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned long ie1;
347
348 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100349 ie1 &= ~SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
351}
352
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100353static void sa1111_unmask_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100355 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800356 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned long ie1;
358
359 ie1 = sa1111_readl(mapbase + SA1111_INTEN1);
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100360 ie1 |= SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 sa1111_writel(ie1, mapbase + SA1111_INTEN1);
362}
363
364/*
365 * Attempt to re-trigger the interrupt. The SA1111 contains a register
366 * (INTSET) which claims to do this. However, in practice no amount of
367 * manipulation of INTEN and INTSET guarantees that the interrupt will
368 * be triggered. In fact, its very difficult, if not impossible to get
369 * INTSET to re-trigger the interrupt.
370 */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100371static int sa1111_retrigger_highirq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100373 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800374 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100375 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned long ip1;
377 int i;
378
379 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
380 for (i = 0; i < 8; i++) {
381 sa1111_writel(ip1 ^ mask, mapbase + SA1111_INTPOL1);
382 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
383 if (sa1111_readl(mapbase + SA1111_INTSTATCLR1) & mask)
384 break;
385 }
386
387 if (i == 8)
Russell King4ed89f22014-10-28 11:26:42 +0000388 pr_err("Danger Will Robinson: failed to re-trigger IRQ%d\n",
389 d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return i == 8 ? -1 : 0;
391}
392
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100393static int sa1111_type_highirq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100395 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800396 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100397 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned long ip1;
399
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100400 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100403 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -EINVAL;
405
406 ip1 = sa1111_readl(mapbase + SA1111_INTPOL1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100407 if (flags & IRQ_TYPE_EDGE_RISING)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 ip1 &= ~mask;
409 else
410 ip1 |= mask;
411 sa1111_writel(ip1, mapbase + SA1111_INTPOL1);
412 sa1111_writel(ip1, mapbase + SA1111_WAKEPOL1);
413
414 return 0;
415}
416
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100417static int sa1111_wake_highirq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100419 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Eric Miao19851c52009-12-26 16:23:02 +0800420 void __iomem *mapbase = sachip->base + SA1111_INTC;
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100421 unsigned int mask = SA1111_IRQMASK_HI(d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long we1;
423
424 we1 = sa1111_readl(mapbase + SA1111_WAKEEN1);
425 if (on)
426 we1 |= mask;
427 else
428 we1 &= ~mask;
429 sa1111_writel(we1, mapbase + SA1111_WAKEEN1);
430
431 return 0;
432}
433
David Brownell38c677c2006-08-01 22:26:25 +0100434static struct irq_chip sa1111_high_chip = {
435 .name = "SA1111-h",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100436 .irq_ack = sa1111_ack_irq,
437 .irq_mask = sa1111_mask_highirq,
438 .irq_unmask = sa1111_unmask_highirq,
439 .irq_retrigger = sa1111_retrigger_highirq,
440 .irq_set_type = sa1111_type_highirq,
441 .irq_set_wake = sa1111_wake_highirq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Russell King36d31212012-01-24 21:25:20 +0000444static int sa1111_setup_irq(struct sa1111 *sachip, unsigned irq_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 void __iomem *irqbase = sachip->base + SA1111_INTC;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000447 unsigned i, irq;
Russell King36d31212012-01-24 21:25:20 +0000448 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /*
451 * We're guaranteed that this region hasn't been taken.
452 */
453 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
454
Russell King36d31212012-01-24 21:25:20 +0000455 ret = irq_alloc_descs(-1, irq_base, SA1111_IRQ_NR, -1);
456 if (ret <= 0) {
457 dev_err(sachip->dev, "unable to allocate %u irqs: %d\n",
458 SA1111_IRQ_NR, ret);
459 if (ret == 0)
460 ret = -EINVAL;
461 return ret;
462 }
463
464 sachip->irq_base = ret;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* disable all IRQs */
467 sa1111_writel(0, irqbase + SA1111_INTEN0);
468 sa1111_writel(0, irqbase + SA1111_INTEN1);
469 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
470 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
471
472 /*
473 * detect on rising edge. Note: Feb 2001 Errata for SA1111
474 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
475 */
476 sa1111_writel(0, irqbase + SA1111_INTPOL0);
Russell King7c0091e2016-09-06 14:21:46 +0100477 sa1111_writel(BIT(IRQ_S0_READY_NINT & 31) |
478 BIT(IRQ_S1_READY_NINT & 31),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 irqbase + SA1111_INTPOL1);
480
481 /* clear all IRQs */
482 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR0);
483 sa1111_writel(~0, irqbase + SA1111_INTSTATCLR1);
484
Russell Kingf03ecaa2012-01-16 00:09:22 +0000485 for (i = IRQ_GPAIN0; i <= SSPROR; i++) {
486 irq = sachip->irq_base + i;
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100487 irq_set_chip_and_handler(irq, &sa1111_low_chip,
488 handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100489 irq_set_chip_data(irq, sachip);
Rob Herringe8d36d52015-07-27 15:55:13 -0500490 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Russell Kingf03ecaa2012-01-16 00:09:22 +0000493 for (i = AUDXMTDMADONEA; i <= IRQ_S1_BVD1_STSCHG; i++) {
494 irq = sachip->irq_base + i;
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100495 irq_set_chip_and_handler(irq, &sa1111_high_chip,
496 handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100497 irq_set_chip_data(irq, sachip);
Rob Herringe8d36d52015-07-27 15:55:13 -0500498 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 /*
502 * Register SA1111 interrupt
503 */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100504 irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
Russell King056c0ac2015-06-16 23:06:25 +0100505 irq_set_chained_handler_and_data(sachip->irq, sa1111_irq_handler,
506 sachip);
Russell King36d31212012-01-24 21:25:20 +0000507
508 dev_info(sachip->dev, "Providing IRQ%u-%u\n",
509 sachip->irq_base, sachip->irq_base + SA1111_IRQ_NR - 1);
510
511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Russell Kingccb7d852016-09-06 16:01:32 +0100514static void sa1111_remove_irq(struct sa1111 *sachip)
515{
516 void __iomem *irqbase = sachip->base + SA1111_INTC;
517
518 /* disable all IRQs */
519 sa1111_writel(0, irqbase + SA1111_INTEN0);
520 sa1111_writel(0, irqbase + SA1111_INTEN1);
521 sa1111_writel(0, irqbase + SA1111_WAKEEN0);
522 sa1111_writel(0, irqbase + SA1111_WAKEEN1);
523
524 if (sachip->irq != NO_IRQ) {
525 irq_set_chained_handler_and_data(sachip->irq, NULL, NULL);
526 irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
527
528 release_mem_region(sachip->phys + SA1111_INTC, 512);
529 }
530}
531
Russell King17cf5012016-08-31 08:49:45 +0100532enum {
533 SA1111_GPIO_PXDDR = (SA1111_GPIO_PADDR - SA1111_GPIO_PADDR),
534 SA1111_GPIO_PXDRR = (SA1111_GPIO_PADRR - SA1111_GPIO_PADDR),
535 SA1111_GPIO_PXDWR = (SA1111_GPIO_PADWR - SA1111_GPIO_PADDR),
536 SA1111_GPIO_PXSDR = (SA1111_GPIO_PASDR - SA1111_GPIO_PADDR),
537 SA1111_GPIO_PXSSR = (SA1111_GPIO_PASSR - SA1111_GPIO_PADDR),
538};
539
540static struct sa1111 *gc_to_sa1111(struct gpio_chip *gc)
541{
542 return container_of(gc, struct sa1111, gc);
543}
544
545static void __iomem *sa1111_gpio_map_reg(struct sa1111 *sachip, unsigned offset)
546{
547 void __iomem *reg = sachip->base + SA1111_GPIO;
548
549 if (offset < 4)
550 return reg + SA1111_GPIO_PADDR;
551 if (offset < 10)
552 return reg + SA1111_GPIO_PBDDR;
553 if (offset < 18)
554 return reg + SA1111_GPIO_PCDDR;
555 return NULL;
556}
557
558static u32 sa1111_gpio_map_bit(unsigned offset)
559{
560 if (offset < 4)
561 return BIT(offset);
562 if (offset < 10)
563 return BIT(offset - 4);
564 if (offset < 18)
565 return BIT(offset - 10);
566 return 0;
567}
568
569static void sa1111_gpio_modify(void __iomem *reg, u32 mask, u32 set)
570{
571 u32 val;
572
573 val = readl_relaxed(reg);
574 val &= ~mask;
575 val |= mask & set;
576 writel_relaxed(val, reg);
577}
578
579static int sa1111_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
580{
581 struct sa1111 *sachip = gc_to_sa1111(gc);
582 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
583 u32 mask = sa1111_gpio_map_bit(offset);
584
585 return !!(readl_relaxed(reg + SA1111_GPIO_PXDDR) & mask);
586}
587
588static int sa1111_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
589{
590 struct sa1111 *sachip = gc_to_sa1111(gc);
591 unsigned long flags;
592 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
593 u32 mask = sa1111_gpio_map_bit(offset);
594
595 spin_lock_irqsave(&sachip->lock, flags);
596 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, mask);
597 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, mask);
598 spin_unlock_irqrestore(&sachip->lock, flags);
599
600 return 0;
601}
602
603static int sa1111_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
604 int value)
605{
606 struct sa1111 *sachip = gc_to_sa1111(gc);
607 unsigned long flags;
608 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
609 u32 mask = sa1111_gpio_map_bit(offset);
610
611 spin_lock_irqsave(&sachip->lock, flags);
612 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
613 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
614 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, 0);
615 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, 0);
616 spin_unlock_irqrestore(&sachip->lock, flags);
617
618 return 0;
619}
620
621static int sa1111_gpio_get(struct gpio_chip *gc, unsigned offset)
622{
623 struct sa1111 *sachip = gc_to_sa1111(gc);
624 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
625 u32 mask = sa1111_gpio_map_bit(offset);
626
627 return !!(readl_relaxed(reg + SA1111_GPIO_PXDRR) & mask);
628}
629
630static void sa1111_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
631{
632 struct sa1111 *sachip = gc_to_sa1111(gc);
633 unsigned long flags;
634 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
635 u32 mask = sa1111_gpio_map_bit(offset);
636
637 spin_lock_irqsave(&sachip->lock, flags);
638 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
639 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
640 spin_unlock_irqrestore(&sachip->lock, flags);
641}
642
643static void sa1111_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
644 unsigned long *bits)
645{
646 struct sa1111 *sachip = gc_to_sa1111(gc);
647 unsigned long flags;
648 void __iomem *reg = sachip->base + SA1111_GPIO;
649 u32 msk, val;
650
651 msk = *mask;
652 val = *bits;
653
654 spin_lock_irqsave(&sachip->lock, flags);
655 sa1111_gpio_modify(reg + SA1111_GPIO_PADWR, msk & 15, val);
656 sa1111_gpio_modify(reg + SA1111_GPIO_PASSR, msk & 15, val);
657 sa1111_gpio_modify(reg + SA1111_GPIO_PBDWR, (msk >> 4) & 255, val >> 4);
658 sa1111_gpio_modify(reg + SA1111_GPIO_PBSSR, (msk >> 4) & 255, val >> 4);
659 sa1111_gpio_modify(reg + SA1111_GPIO_PCDWR, (msk >> 12) & 255, val >> 12);
660 sa1111_gpio_modify(reg + SA1111_GPIO_PCSSR, (msk >> 12) & 255, val >> 12);
661 spin_unlock_irqrestore(&sachip->lock, flags);
662}
663
664static int sa1111_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
665{
666 struct sa1111 *sachip = gc_to_sa1111(gc);
667
668 return sachip->irq_base + offset;
669}
670
671static int sa1111_setup_gpios(struct sa1111 *sachip)
672{
673 sachip->gc.label = "sa1111";
674 sachip->gc.parent = sachip->dev;
675 sachip->gc.owner = THIS_MODULE;
676 sachip->gc.get_direction = sa1111_gpio_get_direction;
677 sachip->gc.direction_input = sa1111_gpio_direction_input;
678 sachip->gc.direction_output = sa1111_gpio_direction_output;
679 sachip->gc.get = sa1111_gpio_get;
680 sachip->gc.set = sa1111_gpio_set;
681 sachip->gc.set_multiple = sa1111_gpio_set_multiple;
682 sachip->gc.to_irq = sa1111_gpio_to_irq;
683 sachip->gc.base = -1;
684 sachip->gc.ngpio = 18;
685
686 return devm_gpiochip_add_data(sachip->dev, &sachip->gc, sachip);
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
690 * Bring the SA1111 out of reset. This requires a set procedure:
691 * 1. nRESET asserted (by hardware)
692 * 2. CLK turned on from SA1110
693 * 3. nRESET deasserted
694 * 4. VCO turned on, PLL_BYPASS turned off
695 * 5. Wait lock time, then assert RCLKEn
696 * 7. PCR set to allow clocking of individual functions
697 *
698 * Until we've done this, the only registers we can access are:
699 * SBI_SKCR
700 * SBI_SMCR
701 * SBI_SKID
702 */
703static void sa1111_wake(struct sa1111 *sachip)
704{
705 unsigned long flags, r;
706
707 spin_lock_irqsave(&sachip->lock, flags);
708
Russell King97d654f2006-03-15 15:54:37 +0000709 clk_enable(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /*
712 * Turn VCO on, and disable PLL Bypass.
713 */
714 r = sa1111_readl(sachip->base + SA1111_SKCR);
715 r &= ~SKCR_VCO_OFF;
716 sa1111_writel(r, sachip->base + SA1111_SKCR);
717 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
718 sa1111_writel(r, sachip->base + SA1111_SKCR);
719
720 /*
721 * Wait lock time. SA1111 manual _doesn't_
722 * specify a figure for this! We choose 100us.
723 */
724 udelay(100);
725
726 /*
727 * Enable RCLK. We also ensure that RDYEN is set.
728 */
729 r |= SKCR_RCLKEN | SKCR_RDYEN;
730 sa1111_writel(r, sachip->base + SA1111_SKCR);
731
732 /*
733 * Wait 14 RCLK cycles for the chip to finish coming out
734 * of reset. (RCLK=24MHz). This is 590ns.
735 */
736 udelay(1);
737
738 /*
739 * Ensure all clocks are initially off.
740 */
741 sa1111_writel(0, sachip->base + SA1111_SKPCR);
742
743 spin_unlock_irqrestore(&sachip->lock, flags);
744}
745
746#ifdef CONFIG_ARCH_SA1100
747
748static u32 sa1111_dma_mask[] = {
749 ~0,
750 ~(1 << 20),
751 ~(1 << 23),
752 ~(1 << 24),
753 ~(1 << 25),
754 ~(1 << 20),
755 ~(1 << 20),
756 0,
757};
758
759/*
760 * Configure the SA1111 shared memory controller.
761 */
762void
763sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
764 unsigned int cas_latency)
765{
766 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
767
768 if (cas_latency == 3)
769 smcr |= SMCR_CLAT;
770
771 sa1111_writel(smcr, sachip->base + SA1111_SMCR);
772
773 /*
774 * Now clear the bits in the DMA mask to work around the SA1111
775 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
776 * Chip Specification Update, June 2000, Erratum #7).
777 */
778 if (sachip->dev->dma_mask)
779 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
780
781 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
782}
Russell King0703ed22011-07-04 08:32:21 +0100783#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static void sa1111_dev_release(struct device *_dev)
786{
787 struct sa1111_dev *dev = SA1111_DEV(_dev);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 kfree(dev);
790}
791
792static int
793sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
794 struct sa1111_dev_info *info)
795{
796 struct sa1111_dev *dev;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000797 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int ret;
799
Russell Kingd2a02b92006-03-20 19:46:41 +0000800 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!dev) {
802 ret = -ENOMEM;
Russell King924e1d42012-01-29 10:20:00 +0000803 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Russell King924e1d42012-01-29 10:20:00 +0000806 device_initialize(&dev->dev);
Kay Sievers3f978702008-05-30 17:42:11 +0200807 dev_set_name(&dev->dev, "%4.4lx", info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dev->devid = info->devid;
809 dev->dev.parent = sachip->dev;
810 dev->dev.bus = &sa1111_bus_type;
811 dev->dev.release = sa1111_dev_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 dev->res.start = sachip->phys + info->offset;
813 dev->res.end = dev->res.start + 511;
Kay Sievers3f978702008-05-30 17:42:11 +0200814 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 dev->res.flags = IORESOURCE_MEM;
816 dev->mapbase = sachip->base + info->offset;
817 dev->skpcr_mask = info->skpcr_mask;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000818
819 for (i = 0; i < ARRAY_SIZE(info->irq); i++)
820 dev->irq[i] = sachip->irq_base + info->irq[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Russell King09a2ba22012-01-29 09:31:31 +0000822 /*
Russell King21d1c772012-01-29 10:22:49 +0000823 * If the parent device has a DMA mask associated with it, and
824 * this child supports DMA, propagate it down to the children.
Russell King09a2ba22012-01-29 09:31:31 +0000825 */
Russell King21d1c772012-01-29 10:22:49 +0000826 if (info->dma && sachip->dev->dma_mask) {
Russell King09a2ba22012-01-29 09:31:31 +0000827 dev->dma_mask = *sachip->dev->dma_mask;
828 dev->dev.dma_mask = &dev->dma_mask;
829 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ret = request_resource(parent, &dev->res);
833 if (ret) {
Russell King22eeaff2012-01-29 10:28:09 +0000834 dev_err(sachip->dev, "failed to allocate resource for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 dev->res.name);
Russell King924e1d42012-01-29 10:20:00 +0000836 goto err_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Russell King924e1d42012-01-29 10:20:00 +0000839 ret = device_add(&dev->dev);
840 if (ret)
841 goto err_add;
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Russell King924e1d42012-01-29 10:20:00 +0000844 err_add:
845 release_resource(&dev->res);
846 err_resource:
847 put_device(&dev->dev);
848 err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
852/**
853 * sa1111_probe - probe for a single SA1111 chip.
854 * @phys_addr: physical address of device.
855 *
856 * Probe for a SA1111 chip. This must be called
857 * before any other SA1111-specific code.
858 *
859 * Returns:
860 * %-ENODEV device not found.
861 * %-EBUSY physical address already marked in-use.
Russell Kingf03ecaa2012-01-16 00:09:22 +0000862 * %-EINVAL no platform data passed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * %0 successful.
864 */
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800865static int __sa1111_probe(struct device *me, struct resource *mem, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Russell Kingf03ecaa2012-01-16 00:09:22 +0000867 struct sa1111_platform_data *pd = me->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct sa1111 *sachip;
869 unsigned long id;
David Brownell416112f2006-08-27 13:09:14 +0100870 unsigned int has_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int i, ret = -ENODEV;
872
Russell Kingf03ecaa2012-01-16 00:09:22 +0000873 if (!pd)
874 return -EINVAL;
875
Russell King7d53c1f2016-09-06 15:29:26 +0100876 sachip = devm_kzalloc(me, sizeof(struct sa1111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!sachip)
878 return -ENOMEM;
879
Russell Kingdeee8562016-08-30 22:47:35 +0100880 sachip->clk = devm_clk_get(me, "SA1111_CLK");
881 if (IS_ERR(sachip->clk))
882 return PTR_ERR(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000883
Russell King72ae00c2011-09-22 11:41:07 +0100884 ret = clk_prepare(sachip->clk);
885 if (ret)
Russell Kingdeee8562016-08-30 22:47:35 +0100886 return ret;
Russell King72ae00c2011-09-22 11:41:07 +0100887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 spin_lock_init(&sachip->lock);
889
890 sachip->dev = me;
891 dev_set_drvdata(sachip->dev, sachip);
892
Russell Kingae99ddb2012-01-26 13:25:47 +0000893 sachip->pdata = pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 sachip->phys = mem->start;
895 sachip->irq = irq;
896
897 /*
898 * Map the whole region. This also maps the
899 * registers for our children.
900 */
901 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
902 if (!sachip->base) {
903 ret = -ENOMEM;
Russell King72ae00c2011-09-22 11:41:07 +0100904 goto err_clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
907 /*
908 * Probe for the chip. Only touch the SBI registers.
909 */
910 id = sa1111_readl(sachip->base + SA1111_SKID);
911 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
912 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
913 ret = -ENODEV;
Russell King97d654f2006-03-15 15:54:37 +0000914 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Russell King4ed89f22014-10-28 11:26:42 +0000917 pr_info("SA1111 Microprocessor Companion Chip: silicon revision %lx, metal revision %lx\n",
918 (id & SKID_SIREV_MASK) >> 4, id & SKID_MTREV_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /*
921 * We found it. Wake the chip up, and initialise.
922 */
923 sa1111_wake(sachip);
924
Russell King36d31212012-01-24 21:25:20 +0000925 /*
926 * The interrupt controller must be initialised before any
927 * other device to ensure that the interrupts are available.
928 */
929 if (sachip->irq != NO_IRQ) {
930 ret = sa1111_setup_irq(sachip, pd->irq_base);
931 if (ret)
Russell King87d5dd62016-09-06 16:09:17 +0100932 goto err_clk;
Russell King36d31212012-01-24 21:25:20 +0000933 }
934
Russell King17cf5012016-08-31 08:49:45 +0100935 /* Setup the GPIOs - should really be done after the IRQ setup */
936 ret = sa1111_setup_gpios(sachip);
937 if (ret)
938 goto err_irq;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940#ifdef CONFIG_ARCH_SA1100
David Brownell416112f2006-08-27 13:09:14 +0100941 {
942 unsigned int val;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * The SDRAM configuration of the SA1110 and the SA1111 must
946 * match. This is very important to ensure that SA1111 accesses
947 * don't corrupt the SDRAM. Note that this ungates the SA1111's
948 * MBGNT signal, so we must have called sa1110_mb_disable()
949 * beforehand.
950 */
951 sa1111_configure_smc(sachip, 1,
952 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
953 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
954
955 /*
956 * We only need to turn on DCLK whenever we want to use the
957 * DMA. It can otherwise be held firmly in the off position.
958 * (currently, we always enable it.)
959 */
960 val = sa1111_readl(sachip->base + SA1111_SKPCR);
961 sa1111_writel(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
962
963 /*
964 * Enable the SA1110 memory bus request and grant signals.
965 */
966 sa1110_mb_enable();
David Brownell416112f2006-08-27 13:09:14 +0100967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968#endif
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 g_sa1111 = sachip;
971
972 has_devs = ~0;
Russell King07be45f2012-01-26 13:34:21 +0000973 if (pd)
974 has_devs &= ~pd->disable_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
Russell Kinge5c0fc42012-01-25 10:42:52 +0000977 if (sa1111_devices[i].devid & has_devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
979
980 return 0;
981
Russell King17cf5012016-08-31 08:49:45 +0100982 err_irq:
983 sa1111_remove_irq(sachip);
Russell King87d5dd62016-09-06 16:09:17 +0100984 err_clk:
985 clk_disable(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000986 err_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 iounmap(sachip->base);
Russell King72ae00c2011-09-22 11:41:07 +0100988 err_clk_unprep:
989 clk_unprepare(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return ret;
991}
992
Russell King522c37b2005-06-22 09:52:26 +0100993static int sa1111_remove_one(struct device *dev, void *data)
994{
Russell King924e1d42012-01-29 10:20:00 +0000995 struct sa1111_dev *sadev = SA1111_DEV(dev);
Russell Kingeac8dbf2016-09-03 09:14:58 +0100996 if (dev->bus != &sa1111_bus_type)
997 return 0;
Russell King924e1d42012-01-29 10:20:00 +0000998 device_del(&sadev->dev);
999 release_resource(&sadev->res);
1000 put_device(&sadev->dev);
Russell King522c37b2005-06-22 09:52:26 +01001001 return 0;
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004static void __sa1111_remove(struct sa1111 *sachip)
1005{
Russell King522c37b2005-06-22 09:52:26 +01001006 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Russell Kingccb7d852016-09-06 16:01:32 +01001008 sa1111_remove_irq(sachip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Russell King97d654f2006-03-15 15:54:37 +00001010 clk_disable(sachip->clk);
Russell King72ae00c2011-09-22 11:41:07 +01001011 clk_unprepare(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +00001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 iounmap(sachip->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016struct sa1111_save_data {
1017 unsigned int skcr;
1018 unsigned int skpcr;
1019 unsigned int skcdr;
1020 unsigned char skaud;
1021 unsigned char skpwm0;
1022 unsigned char skpwm1;
1023
1024 /*
1025 * Interrupt controller
1026 */
1027 unsigned int intpol0;
1028 unsigned int intpol1;
1029 unsigned int inten0;
1030 unsigned int inten1;
1031 unsigned int wakepol0;
1032 unsigned int wakepol1;
1033 unsigned int wakeen0;
1034 unsigned int wakeen1;
1035};
1036
1037#ifdef CONFIG_PM
1038
Russell King06dfe5c2016-09-06 14:34:05 +01001039static int sa1111_suspend_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Russell King06dfe5c2016-09-06 14:34:05 +01001041 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct sa1111_save_data *save;
1043 unsigned long flags;
1044 unsigned int val;
1045 void __iomem *base;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
1048 if (!save)
1049 return -ENOMEM;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001050 sachip->saved_state = save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 spin_lock_irqsave(&sachip->lock, flags);
1053
1054 /*
1055 * Save state.
1056 */
1057 base = sachip->base;
1058 save->skcr = sa1111_readl(base + SA1111_SKCR);
1059 save->skpcr = sa1111_readl(base + SA1111_SKPCR);
1060 save->skcdr = sa1111_readl(base + SA1111_SKCDR);
1061 save->skaud = sa1111_readl(base + SA1111_SKAUD);
1062 save->skpwm0 = sa1111_readl(base + SA1111_SKPWM0);
1063 save->skpwm1 = sa1111_readl(base + SA1111_SKPWM1);
1064
Russell Kinga22db0f32012-02-03 19:05:13 +00001065 sa1111_writel(0, sachip->base + SA1111_SKPWM0);
1066 sa1111_writel(0, sachip->base + SA1111_SKPWM1);
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 base = sachip->base + SA1111_INTC;
1069 save->intpol0 = sa1111_readl(base + SA1111_INTPOL0);
1070 save->intpol1 = sa1111_readl(base + SA1111_INTPOL1);
1071 save->inten0 = sa1111_readl(base + SA1111_INTEN0);
1072 save->inten1 = sa1111_readl(base + SA1111_INTEN1);
1073 save->wakepol0 = sa1111_readl(base + SA1111_WAKEPOL0);
1074 save->wakepol1 = sa1111_readl(base + SA1111_WAKEPOL1);
1075 save->wakeen0 = sa1111_readl(base + SA1111_WAKEEN0);
1076 save->wakeen1 = sa1111_readl(base + SA1111_WAKEEN1);
1077
1078 /*
1079 * Disable.
1080 */
1081 val = sa1111_readl(sachip->base + SA1111_SKCR);
1082 sa1111_writel(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Russell King97d654f2006-03-15 15:54:37 +00001084 clk_disable(sachip->clk);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_unlock_irqrestore(&sachip->lock, flags);
1087
Russell King29c140b2012-02-03 19:03:31 +00001088#ifdef CONFIG_ARCH_SA1100
1089 sa1110_mb_disable();
1090#endif
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return 0;
1093}
1094
1095/*
1096 * sa1111_resume - Restore the SA1111 device state.
1097 * @dev: device to restore
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *
1099 * Restore the general state of the SA1111; clock control and
1100 * interrupt controller. Other parts of the SA1111 must be
1101 * restored by their respective drivers, and must be called
1102 * via LDM after this function.
1103 */
Russell King06dfe5c2016-09-06 14:34:05 +01001104static int sa1111_resume_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Russell King06dfe5c2016-09-06 14:34:05 +01001106 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct sa1111_save_data *save;
1108 unsigned long flags, id;
1109 void __iomem *base;
1110
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001111 save = sachip->saved_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!save)
1113 return 0;
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * Ensure that the SA1111 is still here.
1117 * FIXME: shouldn't do this here.
1118 */
1119 id = sa1111_readl(sachip->base + SA1111_SKID);
1120 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
1121 __sa1111_remove(sachip);
Russell King06dfe5c2016-09-06 14:34:05 +01001122 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 kfree(save);
1124 return 0;
1125 }
1126
1127 /*
1128 * First of all, wake up the chip.
1129 */
1130 sa1111_wake(sachip);
Marek Vašut3defb242010-05-26 23:53:09 +01001131
Russell King29c140b2012-02-03 19:03:31 +00001132#ifdef CONFIG_ARCH_SA1100
1133 /* Enable the memory bus request/grant signals */
1134 sa1110_mb_enable();
1135#endif
1136
Marek Vašut3defb242010-05-26 23:53:09 +01001137 /*
1138 * Only lock for write ops. Also, sa1111_wake must be called with
1139 * released spinlock!
1140 */
1141 spin_lock_irqsave(&sachip->lock, flags);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
1144 sa1111_writel(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
1145
1146 base = sachip->base;
1147 sa1111_writel(save->skcr, base + SA1111_SKCR);
1148 sa1111_writel(save->skpcr, base + SA1111_SKPCR);
1149 sa1111_writel(save->skcdr, base + SA1111_SKCDR);
1150 sa1111_writel(save->skaud, base + SA1111_SKAUD);
1151 sa1111_writel(save->skpwm0, base + SA1111_SKPWM0);
1152 sa1111_writel(save->skpwm1, base + SA1111_SKPWM1);
1153
1154 base = sachip->base + SA1111_INTC;
1155 sa1111_writel(save->intpol0, base + SA1111_INTPOL0);
1156 sa1111_writel(save->intpol1, base + SA1111_INTPOL1);
1157 sa1111_writel(save->inten0, base + SA1111_INTEN0);
1158 sa1111_writel(save->inten1, base + SA1111_INTEN1);
1159 sa1111_writel(save->wakepol0, base + SA1111_WAKEPOL0);
1160 sa1111_writel(save->wakepol1, base + SA1111_WAKEPOL1);
1161 sa1111_writel(save->wakeen0, base + SA1111_WAKEEN0);
1162 sa1111_writel(save->wakeen1, base + SA1111_WAKEEN1);
1163
1164 spin_unlock_irqrestore(&sachip->lock, flags);
1165
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001166 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 kfree(save);
1168
1169 return 0;
1170}
1171
1172#else
Russell King06dfe5c2016-09-06 14:34:05 +01001173#define sa1111_suspend_noirq NULL
1174#define sa1111_resume_noirq NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175#endif
1176
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001177static int sa1111_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct resource *mem;
1180 int irq;
1181
1182 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1183 if (!mem)
1184 return -EINVAL;
1185 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +00001186 if (irq < 0)
Russell Kingcb034402016-09-03 10:21:51 +01001187 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Russell King3ae5eae2005-11-09 22:32:44 +00001189 return __sa1111_probe(&pdev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Russell King3ae5eae2005-11-09 22:32:44 +00001192static int sa1111_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Russell King3ae5eae2005-11-09 22:32:44 +00001194 struct sa1111 *sachip = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (sachip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197#ifdef CONFIG_PM
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001198 kfree(sachip->saved_state);
1199 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
Julia Lawallf2d24202010-07-30 17:17:28 +02001201 __sa1111_remove(sachip);
1202 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205 return 0;
1206}
1207
Russell King06dfe5c2016-09-06 14:34:05 +01001208static struct dev_pm_ops sa1111_pm_ops = {
1209 .suspend_noirq = sa1111_suspend_noirq,
1210 .resume_noirq = sa1111_resume_noirq,
1211};
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * Not sure if this should be on the system bus or not yet.
1215 * We really want some way to register a system device at
1216 * the per-machine level, and then have this driver pick
1217 * up the registered devices.
1218 *
1219 * We also need to handle the SDRAM configuration for
1220 * PXA250/SA1110 machine classes.
1221 */
Russell King3ae5eae2005-11-09 22:32:44 +00001222static struct platform_driver sa1111_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 .probe = sa1111_probe,
1224 .remove = sa1111_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001225 .driver = {
1226 .name = "sa1111",
Russell King06dfe5c2016-09-06 14:34:05 +01001227 .pm = &sa1111_pm_ops,
Russell King3ae5eae2005-11-09 22:32:44 +00001228 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229};
1230
1231/*
1232 * Get the parent device driver (us) structure
1233 * from a child function device
1234 */
1235static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1236{
1237 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1238}
1239
1240/*
1241 * The bits in the opdiv field are non-linear.
1242 */
1243static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1244
1245static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1246{
1247 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1248
1249 skcdr = sa1111_readl(sachip->base + SA1111_SKCDR);
1250
1251 fbdiv = (skcdr & 0x007f) + 2;
1252 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1253 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1254
1255 return 3686400 * fbdiv / (ipdiv * opdiv);
1256}
1257
1258/**
1259 * sa1111_pll_clock - return the current PLL clock frequency.
1260 * @sadev: SA1111 function block
1261 *
1262 * BUG: we should look at SKCR. We also blindly believe that
1263 * the chip is being fed with the 3.6864MHz clock.
1264 *
1265 * Returns the PLL clock in Hz.
1266 */
1267unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1268{
1269 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1270
1271 return __sa1111_pll_clock(sachip);
1272}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001273EXPORT_SYMBOL(sa1111_pll_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/**
1276 * sa1111_select_audio_mode - select I2S or AC link mode
1277 * @sadev: SA1111 function block
1278 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1279 *
1280 * Frob the SKCR to select AC Link mode or I2S mode for
1281 * the audio block.
1282 */
1283void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1284{
1285 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1286 unsigned long flags;
1287 unsigned int val;
1288
1289 spin_lock_irqsave(&sachip->lock, flags);
1290
1291 val = sa1111_readl(sachip->base + SA1111_SKCR);
1292 if (mode == SA1111_AUDIO_I2S) {
1293 val &= ~SKCR_SELAC;
1294 } else {
1295 val |= SKCR_SELAC;
1296 }
1297 sa1111_writel(val, sachip->base + SA1111_SKCR);
1298
1299 spin_unlock_irqrestore(&sachip->lock, flags);
1300}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001301EXPORT_SYMBOL(sa1111_select_audio_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303/**
1304 * sa1111_set_audio_rate - set the audio sample rate
1305 * @sadev: SA1111 SAC function block
1306 * @rate: sample rate to select
1307 */
1308int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1309{
1310 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1311 unsigned int div;
1312
1313 if (sadev->devid != SA1111_DEVID_SAC)
1314 return -EINVAL;
1315
1316 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1317 if (div == 0)
1318 div = 1;
1319 if (div > 128)
1320 div = 128;
1321
1322 sa1111_writel(div - 1, sachip->base + SA1111_SKAUD);
1323
1324 return 0;
1325}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001326EXPORT_SYMBOL(sa1111_set_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/**
1329 * sa1111_get_audio_rate - get the audio sample rate
1330 * @sadev: SA1111 SAC function block device
1331 */
1332int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1333{
1334 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1335 unsigned long div;
1336
1337 if (sadev->devid != SA1111_DEVID_SAC)
1338 return -EINVAL;
1339
1340 div = sa1111_readl(sachip->base + SA1111_SKAUD) + 1;
1341
1342 return __sa1111_pll_clock(sachip) / (256 * div);
1343}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001344EXPORT_SYMBOL(sa1111_get_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346void sa1111_set_io_dir(struct sa1111_dev *sadev,
1347 unsigned int bits, unsigned int dir,
1348 unsigned int sleep_dir)
1349{
1350 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1351 unsigned long flags;
1352 unsigned int val;
1353 void __iomem *gpio = sachip->base + SA1111_GPIO;
1354
1355#define MODIFY_BITS(port, mask, dir) \
1356 if (mask) { \
1357 val = sa1111_readl(port); \
1358 val &= ~(mask); \
1359 val |= (dir) & (mask); \
1360 sa1111_writel(val, port); \
1361 }
1362
1363 spin_lock_irqsave(&sachip->lock, flags);
1364 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1365 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1366 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1367
1368 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1369 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1370 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1371 spin_unlock_irqrestore(&sachip->lock, flags);
1372}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001373EXPORT_SYMBOL(sa1111_set_io_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1376{
1377 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1378 unsigned long flags;
1379 unsigned int val;
1380 void __iomem *gpio = sachip->base + SA1111_GPIO;
1381
1382 spin_lock_irqsave(&sachip->lock, flags);
1383 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1384 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1385 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1386 spin_unlock_irqrestore(&sachip->lock, flags);
1387}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001388EXPORT_SYMBOL(sa1111_set_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1391{
1392 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1393 unsigned long flags;
1394 unsigned int val;
1395 void __iomem *gpio = sachip->base + SA1111_GPIO;
1396
1397 spin_lock_irqsave(&sachip->lock, flags);
1398 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1399 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1400 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1401 spin_unlock_irqrestore(&sachip->lock, flags);
1402}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001403EXPORT_SYMBOL(sa1111_set_sleep_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405/*
1406 * Individual device operations.
1407 */
1408
1409/**
1410 * sa1111_enable_device - enable an on-chip SA1111 function block
1411 * @sadev: SA1111 function block device to enable
1412 */
Russell Kingae99ddb2012-01-26 13:25:47 +00001413int sa1111_enable_device(struct sa1111_dev *sadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1416 unsigned long flags;
1417 unsigned int val;
Russell Kingae99ddb2012-01-26 13:25:47 +00001418 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Russell Kingae99ddb2012-01-26 13:25:47 +00001420 if (sachip->pdata && sachip->pdata->enable)
1421 ret = sachip->pdata->enable(sachip->pdata->data, sadev->devid);
1422
1423 if (ret == 0) {
1424 spin_lock_irqsave(&sachip->lock, flags);
1425 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1426 sa1111_writel(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1427 spin_unlock_irqrestore(&sachip->lock, flags);
1428 }
1429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001431EXPORT_SYMBOL(sa1111_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433/**
1434 * sa1111_disable_device - disable an on-chip SA1111 function block
1435 * @sadev: SA1111 function block device to disable
1436 */
1437void sa1111_disable_device(struct sa1111_dev *sadev)
1438{
1439 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1440 unsigned long flags;
1441 unsigned int val;
1442
1443 spin_lock_irqsave(&sachip->lock, flags);
1444 val = sa1111_readl(sachip->base + SA1111_SKPCR);
1445 sa1111_writel(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
1446 spin_unlock_irqrestore(&sachip->lock, flags);
Russell Kingae99ddb2012-01-26 13:25:47 +00001447
1448 if (sachip->pdata && sachip->pdata->disable)
1449 sachip->pdata->disable(sachip->pdata->data, sadev->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001451EXPORT_SYMBOL(sa1111_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453/*
1454 * SA1111 "Register Access Bus."
1455 *
1456 * We model this as a regular bus type, and hang devices directly
1457 * off this.
1458 */
1459static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1460{
1461 struct sa1111_dev *dev = SA1111_DEV(_dev);
1462 struct sa1111_driver *drv = SA1111_DRV(_drv);
1463
Marek Szyprowski17f29d32016-02-15 09:24:05 +01001464 return !!(dev->devid & drv->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467static int sa1111_bus_suspend(struct device *dev, pm_message_t state)
1468{
1469 struct sa1111_dev *sadev = SA1111_DEV(dev);
1470 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1471 int ret = 0;
1472
1473 if (drv && drv->suspend)
1474 ret = drv->suspend(sadev, state);
1475 return ret;
1476}
1477
1478static int sa1111_bus_resume(struct device *dev)
1479{
1480 struct sa1111_dev *sadev = SA1111_DEV(dev);
1481 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1482 int ret = 0;
1483
1484 if (drv && drv->resume)
1485 ret = drv->resume(sadev);
1486 return ret;
1487}
1488
Russell King6bd72f02012-01-26 11:01:18 +00001489static void sa1111_bus_shutdown(struct device *dev)
1490{
1491 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1492
1493 if (drv && drv->shutdown)
1494 drv->shutdown(SA1111_DEV(dev));
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static int sa1111_bus_probe(struct device *dev)
1498{
1499 struct sa1111_dev *sadev = SA1111_DEV(dev);
1500 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1501 int ret = -ENODEV;
1502
1503 if (drv->probe)
1504 ret = drv->probe(sadev);
1505 return ret;
1506}
1507
1508static int sa1111_bus_remove(struct device *dev)
1509{
1510 struct sa1111_dev *sadev = SA1111_DEV(dev);
1511 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1512 int ret = 0;
1513
1514 if (drv->remove)
1515 ret = drv->remove(sadev);
1516 return ret;
1517}
1518
1519struct bus_type sa1111_bus_type = {
1520 .name = "sa1111-rab",
1521 .match = sa1111_match,
Russell King2876ba42006-01-05 14:32:32 +00001522 .probe = sa1111_bus_probe,
1523 .remove = sa1111_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 .suspend = sa1111_bus_suspend,
1525 .resume = sa1111_bus_resume,
Russell King6bd72f02012-01-26 11:01:18 +00001526 .shutdown = sa1111_bus_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527};
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001528EXPORT_SYMBOL(sa1111_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530int sa1111_driver_register(struct sa1111_driver *driver)
1531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 driver->drv.bus = &sa1111_bus_type;
1533 return driver_register(&driver->drv);
1534}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001535EXPORT_SYMBOL(sa1111_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537void sa1111_driver_unregister(struct sa1111_driver *driver)
1538{
1539 driver_unregister(&driver->drv);
1540}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001541EXPORT_SYMBOL(sa1111_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Russell King09a2ba22012-01-29 09:31:31 +00001543#ifdef CONFIG_DMABOUNCE
1544/*
1545 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
1546 * Chip Specification Update" (June 2000), erratum #7, there is a
1547 * significant bug in the SA1111 SDRAM shared memory controller. If
1548 * an access to a region of memory above 1MB relative to the bank base,
1549 * it is important that address bit 10 _NOT_ be asserted. Depending
1550 * on the configuration of the RAM, bit 10 may correspond to one
1551 * of several different (processor-relative) address bits.
1552 *
1553 * This routine only identifies whether or not a given DMA address
1554 * is susceptible to the bug.
1555 *
1556 * This should only get called for sa1111_device types due to the
1557 * way we configure our device dma_masks.
1558 */
1559static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
1560{
1561 /*
1562 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
1563 * User's Guide" mentions that jumpers R51 and R52 control the
1564 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
1565 * SDRAM bank 1 on Neponset). The default configuration selects
1566 * Assabet, so any address in bank 1 is necessarily invalid.
1567 */
1568 return (machine_is_assabet() || machine_is_pfs168()) &&
1569 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
1570}
1571
1572static int sa1111_notifier_call(struct notifier_block *n, unsigned long action,
1573 void *data)
1574{
1575 struct sa1111_dev *dev = SA1111_DEV(data);
1576
1577 switch (action) {
1578 case BUS_NOTIFY_ADD_DEVICE:
1579 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL) {
1580 int ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
1581 sa1111_needs_bounce);
1582 if (ret)
1583 dev_err(&dev->dev, "failed to register with dmabounce: %d\n", ret);
1584 }
1585 break;
1586
1587 case BUS_NOTIFY_DEL_DEVICE:
1588 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL)
1589 dmabounce_unregister_dev(&dev->dev);
1590 break;
1591 }
1592 return NOTIFY_OK;
1593}
1594
1595static struct notifier_block sa1111_bus_notifier = {
1596 .notifier_call = sa1111_notifier_call,
1597};
1598#endif
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600static int __init sa1111_init(void)
1601{
1602 int ret = bus_register(&sa1111_bus_type);
Russell King09a2ba22012-01-29 09:31:31 +00001603#ifdef CONFIG_DMABOUNCE
1604 if (ret == 0)
1605 bus_register_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001608 platform_driver_register(&sa1111_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return ret;
1610}
1611
1612static void __exit sa1111_exit(void)
1613{
Russell King3ae5eae2005-11-09 22:32:44 +00001614 platform_driver_unregister(&sa1111_device_driver);
Russell King09a2ba22012-01-29 09:31:31 +00001615#ifdef CONFIG_DMABOUNCE
1616 bus_unregister_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 bus_unregister(&sa1111_bus_type);
1619}
1620
Russell King72724382005-11-15 19:04:22 +00001621subsys_initcall(sa1111_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622module_exit(sa1111_exit);
1623
1624MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1625MODULE_LICENSE("GPL");