blob: 36c2162c95afd6ed8f338c1d1c36973d0a99e66a [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
Russell Kinga5b549e2016-09-07 15:11:14 +0100207 stat0 = readl_relaxed(mapbase + SA1111_INTSTATCLR0);
208 stat1 = readl_relaxed(mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Russell Kinga5b549e2016-09-07 15:11:14 +0100210 writel_relaxed(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
Russell Kinga5b549e2016-09-07 15:11:14 +0100214 writel_relaxed(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
Russell King1629c9a2016-09-06 18:43:28 +0100236static u32 sa1111_irqmask(struct irq_data *d)
237{
238 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
239
240 return BIT((d->irq - sachip->irq_base) & 31);
241}
242
243static int sa1111_irqbank(struct irq_data *d)
244{
245 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
246
247 return ((d->irq - sachip->irq_base) / 32) * 4;
248}
249
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100250static void sa1111_ack_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252}
253
Russell King1629c9a2016-09-06 18:43:28 +0100254static void sa1111_mask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100256 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100257 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
258 u32 ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Russell Kinga5b549e2016-09-07 15:11:14 +0100260 ie = readl_relaxed(mapbase + SA1111_INTEN0);
Russell King1629c9a2016-09-06 18:43:28 +0100261 ie &= ~sa1111_irqmask(d);
Russell Kinga5b549e2016-09-07 15:11:14 +0100262 writel(ie, mapbase + SA1111_INTEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Russell King1629c9a2016-09-06 18:43:28 +0100265static void sa1111_unmask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100267 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100268 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
269 u32 ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Russell Kinga5b549e2016-09-07 15:11:14 +0100271 ie = readl_relaxed(mapbase + SA1111_INTEN0);
Russell King1629c9a2016-09-06 18:43:28 +0100272 ie |= sa1111_irqmask(d);
Russell Kinga5b549e2016-09-07 15:11:14 +0100273 writel_relaxed(ie, mapbase + SA1111_INTEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * Attempt to re-trigger the interrupt. The SA1111 contains a register
278 * (INTSET) which claims to do this. However, in practice no amount of
279 * manipulation of INTEN and INTSET guarantees that the interrupt will
280 * be triggered. In fact, its very difficult, if not impossible to get
281 * INTSET to re-trigger the interrupt.
282 */
Russell King1629c9a2016-09-06 18:43:28 +0100283static int sa1111_retrigger_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100285 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100286 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
287 u32 ip, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int i;
289
Russell Kinga5b549e2016-09-07 15:11:14 +0100290 ip = readl_relaxed(mapbase + SA1111_INTPOL0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 for (i = 0; i < 8; i++) {
Russell Kinga5b549e2016-09-07 15:11:14 +0100292 writel_relaxed(ip ^ mask, mapbase + SA1111_INTPOL0);
293 writel_relaxed(ip, mapbase + SA1111_INTPOL0);
294 if (readl_relaxed(mapbase + SA1111_INTSTATCLR0) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
296 }
297
298 if (i == 8)
Russell King4ed89f22014-10-28 11:26:42 +0000299 pr_err("Danger Will Robinson: failed to re-trigger IRQ%d\n",
300 d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return i == 8 ? -1 : 0;
302}
303
Russell King1629c9a2016-09-06 18:43:28 +0100304static int sa1111_type_irq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100306 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100307 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
308 u32 ip, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100310 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100313 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -EINVAL;
315
Russell Kinga5b549e2016-09-07 15:11:14 +0100316 ip = readl_relaxed(mapbase + SA1111_INTPOL0);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100317 if (flags & IRQ_TYPE_EDGE_RISING)
Russell King1629c9a2016-09-06 18:43:28 +0100318 ip &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 else
Russell King1629c9a2016-09-06 18:43:28 +0100320 ip |= mask;
Russell Kinga5b549e2016-09-07 15:11:14 +0100321 writel_relaxed(ip, mapbase + SA1111_INTPOL0);
322 writel_relaxed(ip, mapbase + SA1111_WAKEPOL0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return 0;
325}
326
Russell King1629c9a2016-09-06 18:43:28 +0100327static int sa1111_wake_irq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100329 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100330 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
331 u32 we, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Russell Kinga5b549e2016-09-07 15:11:14 +0100333 we = readl_relaxed(mapbase + SA1111_WAKEEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (on)
Russell King1629c9a2016-09-06 18:43:28 +0100335 we |= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 else
Russell King1629c9a2016-09-06 18:43:28 +0100337 we &= ~mask;
Russell Kinga5b549e2016-09-07 15:11:14 +0100338 writel_relaxed(we, mapbase + SA1111_WAKEEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return 0;
341}
342
Russell King1629c9a2016-09-06 18:43:28 +0100343static struct irq_chip sa1111_irq_chip = {
344 .name = "SA1111",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100345 .irq_ack = sa1111_ack_irq,
Russell King1629c9a2016-09-06 18:43:28 +0100346 .irq_mask = sa1111_mask_irq,
347 .irq_unmask = sa1111_unmask_irq,
348 .irq_retrigger = sa1111_retrigger_irq,
349 .irq_set_type = sa1111_type_irq,
350 .irq_set_wake = sa1111_wake_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
352
Russell King36d31212012-01-24 21:25:20 +0000353static int sa1111_setup_irq(struct sa1111 *sachip, unsigned irq_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 void __iomem *irqbase = sachip->base + SA1111_INTC;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000356 unsigned i, irq;
Russell King36d31212012-01-24 21:25:20 +0000357 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * We're guaranteed that this region hasn't been taken.
361 */
362 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
363
Russell King36d31212012-01-24 21:25:20 +0000364 ret = irq_alloc_descs(-1, irq_base, SA1111_IRQ_NR, -1);
365 if (ret <= 0) {
366 dev_err(sachip->dev, "unable to allocate %u irqs: %d\n",
367 SA1111_IRQ_NR, ret);
368 if (ret == 0)
369 ret = -EINVAL;
370 return ret;
371 }
372
373 sachip->irq_base = ret;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* disable all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100376 writel_relaxed(0, irqbase + SA1111_INTEN0);
377 writel_relaxed(0, irqbase + SA1111_INTEN1);
378 writel_relaxed(0, irqbase + SA1111_WAKEEN0);
379 writel_relaxed(0, irqbase + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * detect on rising edge. Note: Feb 2001 Errata for SA1111
383 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
384 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100385 writel_relaxed(0, irqbase + SA1111_INTPOL0);
386 writel_relaxed(BIT(IRQ_S0_READY_NINT & 31) |
387 BIT(IRQ_S1_READY_NINT & 31),
388 irqbase + SA1111_INTPOL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* clear all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100391 writel_relaxed(~0, irqbase + SA1111_INTSTATCLR0);
392 writel_relaxed(~0, irqbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Russell Kingf03ecaa2012-01-16 00:09:22 +0000394 for (i = IRQ_GPAIN0; i <= SSPROR; i++) {
395 irq = sachip->irq_base + i;
Russell King1629c9a2016-09-06 18:43:28 +0100396 irq_set_chip_and_handler(irq, &sa1111_irq_chip, handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100397 irq_set_chip_data(irq, sachip);
Rob Herringe8d36d52015-07-27 15:55:13 -0500398 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Russell Kingf03ecaa2012-01-16 00:09:22 +0000401 for (i = AUDXMTDMADONEA; i <= IRQ_S1_BVD1_STSCHG; i++) {
402 irq = sachip->irq_base + i;
Russell King1629c9a2016-09-06 18:43:28 +0100403 irq_set_chip_and_handler(irq, &sa1111_irq_chip, handle_edge_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100404 irq_set_chip_data(irq, sachip);
Rob Herringe8d36d52015-07-27 15:55:13 -0500405 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 /*
409 * Register SA1111 interrupt
410 */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100411 irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
Russell King056c0ac2015-06-16 23:06:25 +0100412 irq_set_chained_handler_and_data(sachip->irq, sa1111_irq_handler,
413 sachip);
Russell King36d31212012-01-24 21:25:20 +0000414
415 dev_info(sachip->dev, "Providing IRQ%u-%u\n",
416 sachip->irq_base, sachip->irq_base + SA1111_IRQ_NR - 1);
417
418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Russell Kingccb7d852016-09-06 16:01:32 +0100421static void sa1111_remove_irq(struct sa1111 *sachip)
422{
423 void __iomem *irqbase = sachip->base + SA1111_INTC;
424
425 /* disable all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100426 writel_relaxed(0, irqbase + SA1111_INTEN0);
427 writel_relaxed(0, irqbase + SA1111_INTEN1);
428 writel_relaxed(0, irqbase + SA1111_WAKEEN0);
429 writel_relaxed(0, irqbase + SA1111_WAKEEN1);
Russell Kingccb7d852016-09-06 16:01:32 +0100430
431 if (sachip->irq != NO_IRQ) {
432 irq_set_chained_handler_and_data(sachip->irq, NULL, NULL);
433 irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
434
435 release_mem_region(sachip->phys + SA1111_INTC, 512);
436 }
437}
438
Russell King17cf5012016-08-31 08:49:45 +0100439enum {
440 SA1111_GPIO_PXDDR = (SA1111_GPIO_PADDR - SA1111_GPIO_PADDR),
441 SA1111_GPIO_PXDRR = (SA1111_GPIO_PADRR - SA1111_GPIO_PADDR),
442 SA1111_GPIO_PXDWR = (SA1111_GPIO_PADWR - SA1111_GPIO_PADDR),
443 SA1111_GPIO_PXSDR = (SA1111_GPIO_PASDR - SA1111_GPIO_PADDR),
444 SA1111_GPIO_PXSSR = (SA1111_GPIO_PASSR - SA1111_GPIO_PADDR),
445};
446
447static struct sa1111 *gc_to_sa1111(struct gpio_chip *gc)
448{
449 return container_of(gc, struct sa1111, gc);
450}
451
452static void __iomem *sa1111_gpio_map_reg(struct sa1111 *sachip, unsigned offset)
453{
454 void __iomem *reg = sachip->base + SA1111_GPIO;
455
456 if (offset < 4)
457 return reg + SA1111_GPIO_PADDR;
458 if (offset < 10)
459 return reg + SA1111_GPIO_PBDDR;
460 if (offset < 18)
461 return reg + SA1111_GPIO_PCDDR;
462 return NULL;
463}
464
465static u32 sa1111_gpio_map_bit(unsigned offset)
466{
467 if (offset < 4)
468 return BIT(offset);
469 if (offset < 10)
470 return BIT(offset - 4);
471 if (offset < 18)
472 return BIT(offset - 10);
473 return 0;
474}
475
476static void sa1111_gpio_modify(void __iomem *reg, u32 mask, u32 set)
477{
478 u32 val;
479
480 val = readl_relaxed(reg);
481 val &= ~mask;
482 val |= mask & set;
483 writel_relaxed(val, reg);
484}
485
486static int sa1111_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
487{
488 struct sa1111 *sachip = gc_to_sa1111(gc);
489 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
490 u32 mask = sa1111_gpio_map_bit(offset);
491
492 return !!(readl_relaxed(reg + SA1111_GPIO_PXDDR) & mask);
493}
494
495static int sa1111_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
496{
497 struct sa1111 *sachip = gc_to_sa1111(gc);
498 unsigned long flags;
499 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
500 u32 mask = sa1111_gpio_map_bit(offset);
501
502 spin_lock_irqsave(&sachip->lock, flags);
503 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, mask);
504 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, mask);
505 spin_unlock_irqrestore(&sachip->lock, flags);
506
507 return 0;
508}
509
510static int sa1111_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
511 int value)
512{
513 struct sa1111 *sachip = gc_to_sa1111(gc);
514 unsigned long flags;
515 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
516 u32 mask = sa1111_gpio_map_bit(offset);
517
518 spin_lock_irqsave(&sachip->lock, flags);
519 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
520 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
521 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, 0);
522 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, 0);
523 spin_unlock_irqrestore(&sachip->lock, flags);
524
525 return 0;
526}
527
528static int sa1111_gpio_get(struct gpio_chip *gc, unsigned offset)
529{
530 struct sa1111 *sachip = gc_to_sa1111(gc);
531 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
532 u32 mask = sa1111_gpio_map_bit(offset);
533
534 return !!(readl_relaxed(reg + SA1111_GPIO_PXDRR) & mask);
535}
536
537static void sa1111_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
538{
539 struct sa1111 *sachip = gc_to_sa1111(gc);
540 unsigned long flags;
541 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
542 u32 mask = sa1111_gpio_map_bit(offset);
543
544 spin_lock_irqsave(&sachip->lock, flags);
545 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
546 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
547 spin_unlock_irqrestore(&sachip->lock, flags);
548}
549
550static void sa1111_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
551 unsigned long *bits)
552{
553 struct sa1111 *sachip = gc_to_sa1111(gc);
554 unsigned long flags;
555 void __iomem *reg = sachip->base + SA1111_GPIO;
556 u32 msk, val;
557
558 msk = *mask;
559 val = *bits;
560
561 spin_lock_irqsave(&sachip->lock, flags);
562 sa1111_gpio_modify(reg + SA1111_GPIO_PADWR, msk & 15, val);
563 sa1111_gpio_modify(reg + SA1111_GPIO_PASSR, msk & 15, val);
564 sa1111_gpio_modify(reg + SA1111_GPIO_PBDWR, (msk >> 4) & 255, val >> 4);
565 sa1111_gpio_modify(reg + SA1111_GPIO_PBSSR, (msk >> 4) & 255, val >> 4);
566 sa1111_gpio_modify(reg + SA1111_GPIO_PCDWR, (msk >> 12) & 255, val >> 12);
567 sa1111_gpio_modify(reg + SA1111_GPIO_PCSSR, (msk >> 12) & 255, val >> 12);
568 spin_unlock_irqrestore(&sachip->lock, flags);
569}
570
571static int sa1111_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
572{
573 struct sa1111 *sachip = gc_to_sa1111(gc);
574
575 return sachip->irq_base + offset;
576}
577
578static int sa1111_setup_gpios(struct sa1111 *sachip)
579{
580 sachip->gc.label = "sa1111";
581 sachip->gc.parent = sachip->dev;
582 sachip->gc.owner = THIS_MODULE;
583 sachip->gc.get_direction = sa1111_gpio_get_direction;
584 sachip->gc.direction_input = sa1111_gpio_direction_input;
585 sachip->gc.direction_output = sa1111_gpio_direction_output;
586 sachip->gc.get = sa1111_gpio_get;
587 sachip->gc.set = sa1111_gpio_set;
588 sachip->gc.set_multiple = sa1111_gpio_set_multiple;
589 sachip->gc.to_irq = sa1111_gpio_to_irq;
590 sachip->gc.base = -1;
591 sachip->gc.ngpio = 18;
592
593 return devm_gpiochip_add_data(sachip->dev, &sachip->gc, sachip);
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * Bring the SA1111 out of reset. This requires a set procedure:
598 * 1. nRESET asserted (by hardware)
599 * 2. CLK turned on from SA1110
600 * 3. nRESET deasserted
601 * 4. VCO turned on, PLL_BYPASS turned off
602 * 5. Wait lock time, then assert RCLKEn
603 * 7. PCR set to allow clocking of individual functions
604 *
605 * Until we've done this, the only registers we can access are:
606 * SBI_SKCR
607 * SBI_SMCR
608 * SBI_SKID
609 */
610static void sa1111_wake(struct sa1111 *sachip)
611{
612 unsigned long flags, r;
613
614 spin_lock_irqsave(&sachip->lock, flags);
615
Russell King97d654f2006-03-15 15:54:37 +0000616 clk_enable(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /*
619 * Turn VCO on, and disable PLL Bypass.
620 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100621 r = readl_relaxed(sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 r &= ~SKCR_VCO_OFF;
Russell Kinga5b549e2016-09-07 15:11:14 +0100623 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
Russell Kinga5b549e2016-09-07 15:11:14 +0100625 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /*
628 * Wait lock time. SA1111 manual _doesn't_
629 * specify a figure for this! We choose 100us.
630 */
631 udelay(100);
632
633 /*
634 * Enable RCLK. We also ensure that RDYEN is set.
635 */
636 r |= SKCR_RCLKEN | SKCR_RDYEN;
Russell Kinga5b549e2016-09-07 15:11:14 +0100637 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /*
640 * Wait 14 RCLK cycles for the chip to finish coming out
641 * of reset. (RCLK=24MHz). This is 590ns.
642 */
643 udelay(1);
644
645 /*
646 * Ensure all clocks are initially off.
647 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100648 writel_relaxed(0, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 spin_unlock_irqrestore(&sachip->lock, flags);
651}
652
653#ifdef CONFIG_ARCH_SA1100
654
655static u32 sa1111_dma_mask[] = {
656 ~0,
657 ~(1 << 20),
658 ~(1 << 23),
659 ~(1 << 24),
660 ~(1 << 25),
661 ~(1 << 20),
662 ~(1 << 20),
663 0,
664};
665
666/*
667 * Configure the SA1111 shared memory controller.
668 */
669void
670sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
671 unsigned int cas_latency)
672{
673 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
674
675 if (cas_latency == 3)
676 smcr |= SMCR_CLAT;
677
Russell Kinga5b549e2016-09-07 15:11:14 +0100678 writel_relaxed(smcr, sachip->base + SA1111_SMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /*
681 * Now clear the bits in the DMA mask to work around the SA1111
682 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
683 * Chip Specification Update, June 2000, Erratum #7).
684 */
685 if (sachip->dev->dma_mask)
686 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
687
688 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
689}
Russell King0703ed22011-07-04 08:32:21 +0100690#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static void sa1111_dev_release(struct device *_dev)
693{
Russell Kingb60752f2016-09-06 13:46:46 +0100694 struct sa1111_dev *dev = to_sa1111_device(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 kfree(dev);
697}
698
699static int
700sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
701 struct sa1111_dev_info *info)
702{
703 struct sa1111_dev *dev;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000704 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int ret;
706
Russell Kingd2a02b92006-03-20 19:46:41 +0000707 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!dev) {
709 ret = -ENOMEM;
Russell King924e1d42012-01-29 10:20:00 +0000710 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Russell King924e1d42012-01-29 10:20:00 +0000713 device_initialize(&dev->dev);
Kay Sievers3f978702008-05-30 17:42:11 +0200714 dev_set_name(&dev->dev, "%4.4lx", info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 dev->devid = info->devid;
716 dev->dev.parent = sachip->dev;
717 dev->dev.bus = &sa1111_bus_type;
718 dev->dev.release = sa1111_dev_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 dev->res.start = sachip->phys + info->offset;
720 dev->res.end = dev->res.start + 511;
Kay Sievers3f978702008-05-30 17:42:11 +0200721 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dev->res.flags = IORESOURCE_MEM;
723 dev->mapbase = sachip->base + info->offset;
724 dev->skpcr_mask = info->skpcr_mask;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000725
726 for (i = 0; i < ARRAY_SIZE(info->irq); i++)
727 dev->irq[i] = sachip->irq_base + info->irq[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Russell King09a2ba22012-01-29 09:31:31 +0000729 /*
Russell King21d1c772012-01-29 10:22:49 +0000730 * If the parent device has a DMA mask associated with it, and
731 * this child supports DMA, propagate it down to the children.
Russell King09a2ba22012-01-29 09:31:31 +0000732 */
Russell King21d1c772012-01-29 10:22:49 +0000733 if (info->dma && sachip->dev->dma_mask) {
Russell King09a2ba22012-01-29 09:31:31 +0000734 dev->dma_mask = *sachip->dev->dma_mask;
735 dev->dev.dma_mask = &dev->dma_mask;
736 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
737 }
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ret = request_resource(parent, &dev->res);
740 if (ret) {
Russell King22eeaff2012-01-29 10:28:09 +0000741 dev_err(sachip->dev, "failed to allocate resource for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev->res.name);
Russell King924e1d42012-01-29 10:20:00 +0000743 goto err_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Russell King924e1d42012-01-29 10:20:00 +0000746 ret = device_add(&dev->dev);
747 if (ret)
748 goto err_add;
749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Russell King924e1d42012-01-29 10:20:00 +0000751 err_add:
752 release_resource(&dev->res);
753 err_resource:
754 put_device(&dev->dev);
755 err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return ret;
757}
758
759/**
760 * sa1111_probe - probe for a single SA1111 chip.
761 * @phys_addr: physical address of device.
762 *
763 * Probe for a SA1111 chip. This must be called
764 * before any other SA1111-specific code.
765 *
766 * Returns:
767 * %-ENODEV device not found.
768 * %-EBUSY physical address already marked in-use.
Russell Kingf03ecaa2012-01-16 00:09:22 +0000769 * %-EINVAL no platform data passed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * %0 successful.
771 */
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800772static int __sa1111_probe(struct device *me, struct resource *mem, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Russell Kingf03ecaa2012-01-16 00:09:22 +0000774 struct sa1111_platform_data *pd = me->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct sa1111 *sachip;
776 unsigned long id;
David Brownell416112f2006-08-27 13:09:14 +0100777 unsigned int has_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int i, ret = -ENODEV;
779
Russell Kingf03ecaa2012-01-16 00:09:22 +0000780 if (!pd)
781 return -EINVAL;
782
Russell King7d53c1f2016-09-06 15:29:26 +0100783 sachip = devm_kzalloc(me, sizeof(struct sa1111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!sachip)
785 return -ENOMEM;
786
Russell Kingdeee8562016-08-30 22:47:35 +0100787 sachip->clk = devm_clk_get(me, "SA1111_CLK");
788 if (IS_ERR(sachip->clk))
789 return PTR_ERR(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000790
Russell King72ae00c2011-09-22 11:41:07 +0100791 ret = clk_prepare(sachip->clk);
792 if (ret)
Russell Kingdeee8562016-08-30 22:47:35 +0100793 return ret;
Russell King72ae00c2011-09-22 11:41:07 +0100794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 spin_lock_init(&sachip->lock);
796
797 sachip->dev = me;
798 dev_set_drvdata(sachip->dev, sachip);
799
Russell Kingae99ddb2012-01-26 13:25:47 +0000800 sachip->pdata = pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 sachip->phys = mem->start;
802 sachip->irq = irq;
803
804 /*
805 * Map the whole region. This also maps the
806 * registers for our children.
807 */
808 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
809 if (!sachip->base) {
810 ret = -ENOMEM;
Russell King72ae00c2011-09-22 11:41:07 +0100811 goto err_clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 /*
815 * Probe for the chip. Only touch the SBI registers.
816 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100817 id = readl_relaxed(sachip->base + SA1111_SKID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
819 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
820 ret = -ENODEV;
Russell King97d654f2006-03-15 15:54:37 +0000821 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Russell King4ed89f22014-10-28 11:26:42 +0000824 pr_info("SA1111 Microprocessor Companion Chip: silicon revision %lx, metal revision %lx\n",
825 (id & SKID_SIREV_MASK) >> 4, id & SKID_MTREV_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
828 * We found it. Wake the chip up, and initialise.
829 */
830 sa1111_wake(sachip);
831
Russell King36d31212012-01-24 21:25:20 +0000832 /*
833 * The interrupt controller must be initialised before any
834 * other device to ensure that the interrupts are available.
835 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100836 ret = sa1111_setup_irq(sachip, pd->irq_base);
837 if (ret)
838 goto err_clk;
Russell King36d31212012-01-24 21:25:20 +0000839
Russell King17cf5012016-08-31 08:49:45 +0100840 /* Setup the GPIOs - should really be done after the IRQ setup */
841 ret = sa1111_setup_gpios(sachip);
842 if (ret)
843 goto err_irq;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845#ifdef CONFIG_ARCH_SA1100
David Brownell416112f2006-08-27 13:09:14 +0100846 {
847 unsigned int val;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * The SDRAM configuration of the SA1110 and the SA1111 must
851 * match. This is very important to ensure that SA1111 accesses
852 * don't corrupt the SDRAM. Note that this ungates the SA1111's
853 * MBGNT signal, so we must have called sa1110_mb_disable()
854 * beforehand.
855 */
856 sa1111_configure_smc(sachip, 1,
857 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
858 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
859
860 /*
861 * We only need to turn on DCLK whenever we want to use the
862 * DMA. It can otherwise be held firmly in the off position.
863 * (currently, we always enable it.)
864 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100865 val = readl_relaxed(sachip->base + SA1111_SKPCR);
866 writel_relaxed(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /*
869 * Enable the SA1110 memory bus request and grant signals.
870 */
871 sa1110_mb_enable();
David Brownell416112f2006-08-27 13:09:14 +0100872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 g_sa1111 = sachip;
876
877 has_devs = ~0;
Russell King07be45f2012-01-26 13:34:21 +0000878 if (pd)
879 has_devs &= ~pd->disable_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
Russell Kinge5c0fc42012-01-25 10:42:52 +0000882 if (sa1111_devices[i].devid & has_devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
884
885 return 0;
886
Russell King17cf5012016-08-31 08:49:45 +0100887 err_irq:
888 sa1111_remove_irq(sachip);
Russell King87d5dd62016-09-06 16:09:17 +0100889 err_clk:
890 clk_disable(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000891 err_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 iounmap(sachip->base);
Russell King72ae00c2011-09-22 11:41:07 +0100893 err_clk_unprep:
894 clk_unprepare(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896}
897
Russell King522c37b2005-06-22 09:52:26 +0100898static int sa1111_remove_one(struct device *dev, void *data)
899{
Russell Kingb60752f2016-09-06 13:46:46 +0100900 struct sa1111_dev *sadev = to_sa1111_device(dev);
Russell Kingeac8dbf2016-09-03 09:14:58 +0100901 if (dev->bus != &sa1111_bus_type)
902 return 0;
Russell King924e1d42012-01-29 10:20:00 +0000903 device_del(&sadev->dev);
904 release_resource(&sadev->res);
905 put_device(&sadev->dev);
Russell King522c37b2005-06-22 09:52:26 +0100906 return 0;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static void __sa1111_remove(struct sa1111 *sachip)
910{
Russell King522c37b2005-06-22 09:52:26 +0100911 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Russell Kingccb7d852016-09-06 16:01:32 +0100913 sa1111_remove_irq(sachip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Russell King97d654f2006-03-15 15:54:37 +0000915 clk_disable(sachip->clk);
Russell King72ae00c2011-09-22 11:41:07 +0100916 clk_unprepare(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 iounmap(sachip->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921struct sa1111_save_data {
922 unsigned int skcr;
923 unsigned int skpcr;
924 unsigned int skcdr;
925 unsigned char skaud;
926 unsigned char skpwm0;
927 unsigned char skpwm1;
928
929 /*
930 * Interrupt controller
931 */
932 unsigned int intpol0;
933 unsigned int intpol1;
934 unsigned int inten0;
935 unsigned int inten1;
936 unsigned int wakepol0;
937 unsigned int wakepol1;
938 unsigned int wakeen0;
939 unsigned int wakeen1;
940};
941
942#ifdef CONFIG_PM
943
Russell King06dfe5c2016-09-06 14:34:05 +0100944static int sa1111_suspend_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Russell King06dfe5c2016-09-06 14:34:05 +0100946 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct sa1111_save_data *save;
948 unsigned long flags;
949 unsigned int val;
950 void __iomem *base;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
953 if (!save)
954 return -ENOMEM;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700955 sachip->saved_state = save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 spin_lock_irqsave(&sachip->lock, flags);
958
959 /*
960 * Save state.
961 */
962 base = sachip->base;
Russell Kinga5b549e2016-09-07 15:11:14 +0100963 save->skcr = readl_relaxed(base + SA1111_SKCR);
964 save->skpcr = readl_relaxed(base + SA1111_SKPCR);
965 save->skcdr = readl_relaxed(base + SA1111_SKCDR);
966 save->skaud = readl_relaxed(base + SA1111_SKAUD);
967 save->skpwm0 = readl_relaxed(base + SA1111_SKPWM0);
968 save->skpwm1 = readl_relaxed(base + SA1111_SKPWM1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Russell Kinga5b549e2016-09-07 15:11:14 +0100970 writel_relaxed(0, sachip->base + SA1111_SKPWM0);
971 writel_relaxed(0, sachip->base + SA1111_SKPWM1);
Russell Kinga22db0f32012-02-03 19:05:13 +0000972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 base = sachip->base + SA1111_INTC;
Russell Kinga5b549e2016-09-07 15:11:14 +0100974 save->intpol0 = readl_relaxed(base + SA1111_INTPOL0);
975 save->intpol1 = readl_relaxed(base + SA1111_INTPOL1);
976 save->inten0 = readl_relaxed(base + SA1111_INTEN0);
977 save->inten1 = readl_relaxed(base + SA1111_INTEN1);
978 save->wakepol0 = readl_relaxed(base + SA1111_WAKEPOL0);
979 save->wakepol1 = readl_relaxed(base + SA1111_WAKEPOL1);
980 save->wakeen0 = readl_relaxed(base + SA1111_WAKEEN0);
981 save->wakeen1 = readl_relaxed(base + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /*
984 * Disable.
985 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100986 val = readl_relaxed(sachip->base + SA1111_SKCR);
987 writel_relaxed(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Russell King97d654f2006-03-15 15:54:37 +0000989 clk_disable(sachip->clk);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_unlock_irqrestore(&sachip->lock, flags);
992
Russell King29c140b2012-02-03 19:03:31 +0000993#ifdef CONFIG_ARCH_SA1100
994 sa1110_mb_disable();
995#endif
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 0;
998}
999
1000/*
1001 * sa1111_resume - Restore the SA1111 device state.
1002 * @dev: device to restore
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
1004 * Restore the general state of the SA1111; clock control and
1005 * interrupt controller. Other parts of the SA1111 must be
1006 * restored by their respective drivers, and must be called
1007 * via LDM after this function.
1008 */
Russell King06dfe5c2016-09-06 14:34:05 +01001009static int sa1111_resume_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Russell King06dfe5c2016-09-06 14:34:05 +01001011 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct sa1111_save_data *save;
1013 unsigned long flags, id;
1014 void __iomem *base;
1015
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001016 save = sachip->saved_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!save)
1018 return 0;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * Ensure that the SA1111 is still here.
1022 * FIXME: shouldn't do this here.
1023 */
Russell Kinga5b549e2016-09-07 15:11:14 +01001024 id = readl_relaxed(sachip->base + SA1111_SKID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
1026 __sa1111_remove(sachip);
Russell King06dfe5c2016-09-06 14:34:05 +01001027 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 kfree(save);
1029 return 0;
1030 }
1031
1032 /*
1033 * First of all, wake up the chip.
1034 */
1035 sa1111_wake(sachip);
Marek Vašut3defb242010-05-26 23:53:09 +01001036
Russell King29c140b2012-02-03 19:03:31 +00001037#ifdef CONFIG_ARCH_SA1100
1038 /* Enable the memory bus request/grant signals */
1039 sa1110_mb_enable();
1040#endif
1041
Marek Vašut3defb242010-05-26 23:53:09 +01001042 /*
1043 * Only lock for write ops. Also, sa1111_wake must be called with
1044 * released spinlock!
1045 */
1046 spin_lock_irqsave(&sachip->lock, flags);
1047
Russell Kinga5b549e2016-09-07 15:11:14 +01001048 writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
1049 writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 base = sachip->base;
Russell Kinga5b549e2016-09-07 15:11:14 +01001052 writel_relaxed(save->skcr, base + SA1111_SKCR);
1053 writel_relaxed(save->skpcr, base + SA1111_SKPCR);
1054 writel_relaxed(save->skcdr, base + SA1111_SKCDR);
1055 writel_relaxed(save->skaud, base + SA1111_SKAUD);
1056 writel_relaxed(save->skpwm0, base + SA1111_SKPWM0);
1057 writel_relaxed(save->skpwm1, base + SA1111_SKPWM1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 base = sachip->base + SA1111_INTC;
Russell Kinga5b549e2016-09-07 15:11:14 +01001060 writel_relaxed(save->intpol0, base + SA1111_INTPOL0);
1061 writel_relaxed(save->intpol1, base + SA1111_INTPOL1);
1062 writel_relaxed(save->inten0, base + SA1111_INTEN0);
1063 writel_relaxed(save->inten1, base + SA1111_INTEN1);
1064 writel_relaxed(save->wakepol0, base + SA1111_WAKEPOL0);
1065 writel_relaxed(save->wakepol1, base + SA1111_WAKEPOL1);
1066 writel_relaxed(save->wakeen0, base + SA1111_WAKEEN0);
1067 writel_relaxed(save->wakeen1, base + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 spin_unlock_irqrestore(&sachip->lock, flags);
1070
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001071 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 kfree(save);
1073
1074 return 0;
1075}
1076
1077#else
Russell King06dfe5c2016-09-06 14:34:05 +01001078#define sa1111_suspend_noirq NULL
1079#define sa1111_resume_noirq NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#endif
1081
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001082static int sa1111_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct resource *mem;
1085 int irq;
1086
1087 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1088 if (!mem)
1089 return -EINVAL;
1090 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +00001091 if (irq < 0)
Russell Kingcb034402016-09-03 10:21:51 +01001092 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Russell King3ae5eae2005-11-09 22:32:44 +00001094 return __sa1111_probe(&pdev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Russell King3ae5eae2005-11-09 22:32:44 +00001097static int sa1111_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Russell King3ae5eae2005-11-09 22:32:44 +00001099 struct sa1111 *sachip = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (sachip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102#ifdef CONFIG_PM
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001103 kfree(sachip->saved_state);
1104 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#endif
Julia Lawallf2d24202010-07-30 17:17:28 +02001106 __sa1111_remove(sachip);
1107 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 return 0;
1111}
1112
Russell King06dfe5c2016-09-06 14:34:05 +01001113static struct dev_pm_ops sa1111_pm_ops = {
1114 .suspend_noirq = sa1111_suspend_noirq,
1115 .resume_noirq = sa1111_resume_noirq,
1116};
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * Not sure if this should be on the system bus or not yet.
1120 * We really want some way to register a system device at
1121 * the per-machine level, and then have this driver pick
1122 * up the registered devices.
1123 *
1124 * We also need to handle the SDRAM configuration for
1125 * PXA250/SA1110 machine classes.
1126 */
Russell King3ae5eae2005-11-09 22:32:44 +00001127static struct platform_driver sa1111_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 .probe = sa1111_probe,
1129 .remove = sa1111_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001130 .driver = {
1131 .name = "sa1111",
Russell King06dfe5c2016-09-06 14:34:05 +01001132 .pm = &sa1111_pm_ops,
Russell King3ae5eae2005-11-09 22:32:44 +00001133 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134};
1135
1136/*
1137 * Get the parent device driver (us) structure
1138 * from a child function device
1139 */
1140static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1141{
1142 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1143}
1144
1145/*
1146 * The bits in the opdiv field are non-linear.
1147 */
1148static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1149
1150static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1151{
1152 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1153
Russell Kinga5b549e2016-09-07 15:11:14 +01001154 skcdr = readl_relaxed(sachip->base + SA1111_SKCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 fbdiv = (skcdr & 0x007f) + 2;
1157 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1158 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1159
1160 return 3686400 * fbdiv / (ipdiv * opdiv);
1161}
1162
1163/**
1164 * sa1111_pll_clock - return the current PLL clock frequency.
1165 * @sadev: SA1111 function block
1166 *
1167 * BUG: we should look at SKCR. We also blindly believe that
1168 * the chip is being fed with the 3.6864MHz clock.
1169 *
1170 * Returns the PLL clock in Hz.
1171 */
1172unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1173{
1174 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1175
1176 return __sa1111_pll_clock(sachip);
1177}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001178EXPORT_SYMBOL(sa1111_pll_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180/**
1181 * sa1111_select_audio_mode - select I2S or AC link mode
1182 * @sadev: SA1111 function block
1183 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1184 *
1185 * Frob the SKCR to select AC Link mode or I2S mode for
1186 * the audio block.
1187 */
1188void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1189{
1190 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1191 unsigned long flags;
1192 unsigned int val;
1193
1194 spin_lock_irqsave(&sachip->lock, flags);
1195
Russell Kinga5b549e2016-09-07 15:11:14 +01001196 val = readl_relaxed(sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (mode == SA1111_AUDIO_I2S) {
1198 val &= ~SKCR_SELAC;
1199 } else {
1200 val |= SKCR_SELAC;
1201 }
Russell Kinga5b549e2016-09-07 15:11:14 +01001202 writel_relaxed(val, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 spin_unlock_irqrestore(&sachip->lock, flags);
1205}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001206EXPORT_SYMBOL(sa1111_select_audio_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/**
1209 * sa1111_set_audio_rate - set the audio sample rate
1210 * @sadev: SA1111 SAC function block
1211 * @rate: sample rate to select
1212 */
1213int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1214{
1215 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1216 unsigned int div;
1217
1218 if (sadev->devid != SA1111_DEVID_SAC)
1219 return -EINVAL;
1220
1221 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1222 if (div == 0)
1223 div = 1;
1224 if (div > 128)
1225 div = 128;
1226
Russell Kinga5b549e2016-09-07 15:11:14 +01001227 writel_relaxed(div - 1, sachip->base + SA1111_SKAUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 return 0;
1230}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001231EXPORT_SYMBOL(sa1111_set_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233/**
1234 * sa1111_get_audio_rate - get the audio sample rate
1235 * @sadev: SA1111 SAC function block device
1236 */
1237int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1238{
1239 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1240 unsigned long div;
1241
1242 if (sadev->devid != SA1111_DEVID_SAC)
1243 return -EINVAL;
1244
Russell Kinga5b549e2016-09-07 15:11:14 +01001245 div = readl_relaxed(sachip->base + SA1111_SKAUD) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 return __sa1111_pll_clock(sachip) / (256 * div);
1248}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001249EXPORT_SYMBOL(sa1111_get_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251void sa1111_set_io_dir(struct sa1111_dev *sadev,
1252 unsigned int bits, unsigned int dir,
1253 unsigned int sleep_dir)
1254{
1255 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1256 unsigned long flags;
1257 unsigned int val;
1258 void __iomem *gpio = sachip->base + SA1111_GPIO;
1259
1260#define MODIFY_BITS(port, mask, dir) \
1261 if (mask) { \
Russell Kinga5b549e2016-09-07 15:11:14 +01001262 val = readl_relaxed(port); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 val &= ~(mask); \
1264 val |= (dir) & (mask); \
Russell Kinga5b549e2016-09-07 15:11:14 +01001265 writel_relaxed(val, port); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
1268 spin_lock_irqsave(&sachip->lock, flags);
1269 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1270 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1271 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1272
1273 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1274 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1275 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1276 spin_unlock_irqrestore(&sachip->lock, flags);
1277}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001278EXPORT_SYMBOL(sa1111_set_io_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1281{
1282 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1283 unsigned long flags;
1284 unsigned int val;
1285 void __iomem *gpio = sachip->base + SA1111_GPIO;
1286
1287 spin_lock_irqsave(&sachip->lock, flags);
1288 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1289 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1290 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1291 spin_unlock_irqrestore(&sachip->lock, flags);
1292}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001293EXPORT_SYMBOL(sa1111_set_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1296{
1297 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1298 unsigned long flags;
1299 unsigned int val;
1300 void __iomem *gpio = sachip->base + SA1111_GPIO;
1301
1302 spin_lock_irqsave(&sachip->lock, flags);
1303 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1304 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1305 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1306 spin_unlock_irqrestore(&sachip->lock, flags);
1307}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001308EXPORT_SYMBOL(sa1111_set_sleep_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310/*
1311 * Individual device operations.
1312 */
1313
1314/**
1315 * sa1111_enable_device - enable an on-chip SA1111 function block
1316 * @sadev: SA1111 function block device to enable
1317 */
Russell Kingae99ddb2012-01-26 13:25:47 +00001318int sa1111_enable_device(struct sa1111_dev *sadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1321 unsigned long flags;
1322 unsigned int val;
Russell Kingae99ddb2012-01-26 13:25:47 +00001323 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Russell Kingae99ddb2012-01-26 13:25:47 +00001325 if (sachip->pdata && sachip->pdata->enable)
1326 ret = sachip->pdata->enable(sachip->pdata->data, sadev->devid);
1327
1328 if (ret == 0) {
1329 spin_lock_irqsave(&sachip->lock, flags);
Russell Kinga5b549e2016-09-07 15:11:14 +01001330 val = readl_relaxed(sachip->base + SA1111_SKPCR);
1331 writel_relaxed(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
Russell Kingae99ddb2012-01-26 13:25:47 +00001332 spin_unlock_irqrestore(&sachip->lock, flags);
1333 }
1334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001336EXPORT_SYMBOL(sa1111_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/**
1339 * sa1111_disable_device - disable an on-chip SA1111 function block
1340 * @sadev: SA1111 function block device to disable
1341 */
1342void sa1111_disable_device(struct sa1111_dev *sadev)
1343{
1344 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1345 unsigned long flags;
1346 unsigned int val;
1347
1348 spin_lock_irqsave(&sachip->lock, flags);
Russell Kinga5b549e2016-09-07 15:11:14 +01001349 val = readl_relaxed(sachip->base + SA1111_SKPCR);
1350 writel_relaxed(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_unlock_irqrestore(&sachip->lock, flags);
Russell Kingae99ddb2012-01-26 13:25:47 +00001352
1353 if (sachip->pdata && sachip->pdata->disable)
1354 sachip->pdata->disable(sachip->pdata->data, sadev->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001356EXPORT_SYMBOL(sa1111_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Russell Kingcf6e4ca2016-09-06 00:45:33 +01001358int sa1111_get_irq(struct sa1111_dev *sadev, unsigned num)
1359{
1360 if (num >= ARRAY_SIZE(sadev->irq))
1361 return -EINVAL;
1362 return sadev->irq[num];
1363}
1364EXPORT_SYMBOL_GPL(sa1111_get_irq);
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/*
1367 * SA1111 "Register Access Bus."
1368 *
1369 * We model this as a regular bus type, and hang devices directly
1370 * off this.
1371 */
1372static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1373{
Russell Kingb60752f2016-09-06 13:46:46 +01001374 struct sa1111_dev *dev = to_sa1111_device(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 struct sa1111_driver *drv = SA1111_DRV(_drv);
1376
Marek Szyprowski17f29d362016-02-15 09:24:05 +01001377 return !!(dev->devid & drv->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static int sa1111_bus_probe(struct device *dev)
1381{
Russell Kingb60752f2016-09-06 13:46:46 +01001382 struct sa1111_dev *sadev = to_sa1111_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1384 int ret = -ENODEV;
1385
1386 if (drv->probe)
1387 ret = drv->probe(sadev);
1388 return ret;
1389}
1390
1391static int sa1111_bus_remove(struct device *dev)
1392{
Russell Kingb60752f2016-09-06 13:46:46 +01001393 struct sa1111_dev *sadev = to_sa1111_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1395 int ret = 0;
1396
1397 if (drv->remove)
1398 ret = drv->remove(sadev);
1399 return ret;
1400}
1401
1402struct bus_type sa1111_bus_type = {
1403 .name = "sa1111-rab",
1404 .match = sa1111_match,
Russell King2876ba42006-01-05 14:32:32 +00001405 .probe = sa1111_bus_probe,
1406 .remove = sa1111_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407};
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001408EXPORT_SYMBOL(sa1111_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410int sa1111_driver_register(struct sa1111_driver *driver)
1411{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 driver->drv.bus = &sa1111_bus_type;
1413 return driver_register(&driver->drv);
1414}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001415EXPORT_SYMBOL(sa1111_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417void sa1111_driver_unregister(struct sa1111_driver *driver)
1418{
1419 driver_unregister(&driver->drv);
1420}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001421EXPORT_SYMBOL(sa1111_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Russell King09a2ba22012-01-29 09:31:31 +00001423#ifdef CONFIG_DMABOUNCE
1424/*
1425 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
1426 * Chip Specification Update" (June 2000), erratum #7, there is a
1427 * significant bug in the SA1111 SDRAM shared memory controller. If
1428 * an access to a region of memory above 1MB relative to the bank base,
1429 * it is important that address bit 10 _NOT_ be asserted. Depending
1430 * on the configuration of the RAM, bit 10 may correspond to one
1431 * of several different (processor-relative) address bits.
1432 *
1433 * This routine only identifies whether or not a given DMA address
1434 * is susceptible to the bug.
1435 *
1436 * This should only get called for sa1111_device types due to the
1437 * way we configure our device dma_masks.
1438 */
1439static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
1440{
1441 /*
1442 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
1443 * User's Guide" mentions that jumpers R51 and R52 control the
1444 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
1445 * SDRAM bank 1 on Neponset). The default configuration selects
1446 * Assabet, so any address in bank 1 is necessarily invalid.
1447 */
1448 return (machine_is_assabet() || machine_is_pfs168()) &&
1449 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
1450}
1451
1452static int sa1111_notifier_call(struct notifier_block *n, unsigned long action,
1453 void *data)
1454{
Russell Kingb60752f2016-09-06 13:46:46 +01001455 struct sa1111_dev *dev = to_sa1111_device(data);
Russell King09a2ba22012-01-29 09:31:31 +00001456
1457 switch (action) {
1458 case BUS_NOTIFY_ADD_DEVICE:
1459 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL) {
1460 int ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
1461 sa1111_needs_bounce);
1462 if (ret)
1463 dev_err(&dev->dev, "failed to register with dmabounce: %d\n", ret);
1464 }
1465 break;
1466
1467 case BUS_NOTIFY_DEL_DEVICE:
1468 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL)
1469 dmabounce_unregister_dev(&dev->dev);
1470 break;
1471 }
1472 return NOTIFY_OK;
1473}
1474
1475static struct notifier_block sa1111_bus_notifier = {
1476 .notifier_call = sa1111_notifier_call,
1477};
1478#endif
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480static int __init sa1111_init(void)
1481{
1482 int ret = bus_register(&sa1111_bus_type);
Russell King09a2ba22012-01-29 09:31:31 +00001483#ifdef CONFIG_DMABOUNCE
1484 if (ret == 0)
1485 bus_register_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001488 platform_driver_register(&sa1111_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return ret;
1490}
1491
1492static void __exit sa1111_exit(void)
1493{
Russell King3ae5eae2005-11-09 22:32:44 +00001494 platform_driver_unregister(&sa1111_device_driver);
Russell King09a2ba22012-01-29 09:31:31 +00001495#ifdef CONFIG_DMABOUNCE
1496 bus_unregister_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1497#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 bus_unregister(&sa1111_bus_type);
1499}
1500
Russell King72724382005-11-15 19:04:22 +00001501subsys_initcall(sa1111_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502module_exit(sa1111_exit);
1503
1504MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1505MODULE_LICENSE("GPL");