blob: d6a0ce7b51cf59828b90e16f83fa5b9401738708 [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 King4c9f6d32016-09-07 13:50:27 +0100111 struct irq_domain *irqdomain;
Russell King17cf5012016-08-31 08:49:45 +0100112 struct gpio_chip gc;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700113#ifdef CONFIG_PM
114 void *saved_state;
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118/*
119 * We _really_ need to eliminate this. Its only users
120 * are the PWM and DMA checking code.
121 */
122static struct sa1111 *g_sa1111;
123
124struct sa1111_dev_info {
125 unsigned long offset;
126 unsigned long skpcr_mask;
Russell King21d1c772012-01-29 10:22:49 +0000127 bool dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned int devid;
129 unsigned int irq[6];
130};
131
132static struct sa1111_dev_info sa1111_devices[] = {
133 {
134 .offset = SA1111_USB,
135 .skpcr_mask = SKPCR_UCLKEN,
Russell King21d1c772012-01-29 10:22:49 +0000136 .dma = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .devid = SA1111_DEVID_USB,
138 .irq = {
139 IRQ_USBPWR,
140 IRQ_HCIM,
141 IRQ_HCIBUFFACC,
142 IRQ_HCIRMTWKP,
143 IRQ_NHCIMFCIR,
144 IRQ_USB_PORT_RESUME
145 },
146 },
147 {
148 .offset = 0x0600,
149 .skpcr_mask = SKPCR_I2SCLKEN | SKPCR_L3CLKEN,
Russell King21d1c772012-01-29 10:22:49 +0000150 .dma = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .devid = SA1111_DEVID_SAC,
152 .irq = {
153 AUDXMTDMADONEA,
154 AUDXMTDMADONEB,
155 AUDRCVDMADONEA,
156 AUDRCVDMADONEB
157 },
158 },
159 {
160 .offset = 0x0800,
161 .skpcr_mask = SKPCR_SCLKEN,
162 .devid = SA1111_DEVID_SSP,
163 },
164 {
165 .offset = SA1111_KBD,
166 .skpcr_mask = SKPCR_PTCLKEN,
Russell Kinge5c0fc42012-01-25 10:42:52 +0000167 .devid = SA1111_DEVID_PS2_KBD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .irq = {
169 IRQ_TPRXINT,
170 IRQ_TPTXINT
171 },
172 },
173 {
174 .offset = SA1111_MSE,
175 .skpcr_mask = SKPCR_PMCLKEN,
Russell Kinge5c0fc42012-01-25 10:42:52 +0000176 .devid = SA1111_DEVID_PS2_MSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .irq = {
178 IRQ_MSRXINT,
179 IRQ_MSTXINT
180 },
181 },
182 {
183 .offset = 0x1800,
184 .skpcr_mask = 0,
185 .devid = SA1111_DEVID_PCMCIA,
186 .irq = {
187 IRQ_S0_READY_NINT,
188 IRQ_S0_CD_VALID,
189 IRQ_S0_BVD1_STSCHG,
190 IRQ_S1_READY_NINT,
191 IRQ_S1_CD_VALID,
192 IRQ_S1_BVD1_STSCHG,
193 },
194 },
195};
196
Russell King4c9f6d32016-09-07 13:50:27 +0100197static void sa1111_handle_irqdomain(struct irq_domain *irqdomain, int irq)
198{
199 struct irq_desc *d = irq_to_desc(irq_linear_revmap(irqdomain, irq));
200
201 if (d)
202 generic_handle_irq_desc(d);
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * SA1111 interrupt support. Since clearing an IRQ while there are
207 * active IRQs causes the interrupt output to pulse, the upper levels
208 * will call us again if there are more interrupts to process.
209 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200210static void sa1111_irq_handler(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned int stat0, stat1, i;
Jiang Liuf5753982015-06-04 12:13:19 +0800213 struct sa1111 *sachip = irq_desc_get_handler_data(desc);
Russell King4c9f6d32016-09-07 13:50:27 +0100214 struct irq_domain *irqdomain;
Eric Miao19851c52009-12-26 16:23:02 +0800215 void __iomem *mapbase = sachip->base + SA1111_INTC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Russell Kinga5b549e2016-09-07 15:11:14 +0100217 stat0 = readl_relaxed(mapbase + SA1111_INTSTATCLR0);
218 stat1 = readl_relaxed(mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Russell Kinga5b549e2016-09-07 15:11:14 +0100220 writel_relaxed(stat0, mapbase + SA1111_INTSTATCLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100222 desc->irq_data.chip->irq_ack(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Russell Kinga5b549e2016-09-07 15:11:14 +0100224 writel_relaxed(stat1, mapbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (stat0 == 0 && stat1 == 0) {
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200227 do_bad_IRQ(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return;
229 }
230
Russell King4c9f6d32016-09-07 13:50:27 +0100231 irqdomain = sachip->irqdomain;
232
Eric Miao19851c52009-12-26 16:23:02 +0800233 for (i = 0; stat0; i++, stat0 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (stat0 & 1)
Russell King4c9f6d32016-09-07 13:50:27 +0100235 sa1111_handle_irqdomain(irqdomain, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Eric Miao19851c52009-12-26 16:23:02 +0800237 for (i = 32; stat1; i++, stat1 >>= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (stat1 & 1)
Russell King4c9f6d32016-09-07 13:50:27 +0100239 sa1111_handle_irqdomain(irqdomain, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* For level-based interrupts */
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100242 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Russell King1629c9a2016-09-06 18:43:28 +0100245static u32 sa1111_irqmask(struct irq_data *d)
246{
Russell King4c9f6d32016-09-07 13:50:27 +0100247 return BIT(irqd_to_hwirq(d) & 31);
Russell King1629c9a2016-09-06 18:43:28 +0100248}
249
250static int sa1111_irqbank(struct irq_data *d)
251{
Russell King4c9f6d32016-09-07 13:50:27 +0100252 return (irqd_to_hwirq(d) / 32) * 4;
Russell King1629c9a2016-09-06 18:43:28 +0100253}
254
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100255static void sa1111_ack_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257}
258
Russell King1629c9a2016-09-06 18:43:28 +0100259static void sa1111_mask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100261 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100262 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
263 u32 ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Russell Kinga5b549e2016-09-07 15:11:14 +0100265 ie = readl_relaxed(mapbase + SA1111_INTEN0);
Russell King1629c9a2016-09-06 18:43:28 +0100266 ie &= ~sa1111_irqmask(d);
Russell Kinga5b549e2016-09-07 15:11:14 +0100267 writel(ie, mapbase + SA1111_INTEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Russell King1629c9a2016-09-06 18:43:28 +0100270static void sa1111_unmask_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100272 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100273 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
274 u32 ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Russell Kinga5b549e2016-09-07 15:11:14 +0100276 ie = readl_relaxed(mapbase + SA1111_INTEN0);
Russell King1629c9a2016-09-06 18:43:28 +0100277 ie |= sa1111_irqmask(d);
Russell Kinga5b549e2016-09-07 15:11:14 +0100278 writel_relaxed(ie, mapbase + SA1111_INTEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Attempt to re-trigger the interrupt. The SA1111 contains a register
283 * (INTSET) which claims to do this. However, in practice no amount of
284 * manipulation of INTEN and INTSET guarantees that the interrupt will
285 * be triggered. In fact, its very difficult, if not impossible to get
286 * INTSET to re-trigger the interrupt.
287 */
Russell King1629c9a2016-09-06 18:43:28 +0100288static int sa1111_retrigger_irq(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100290 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100291 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
292 u32 ip, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int i;
294
Russell Kinga5b549e2016-09-07 15:11:14 +0100295 ip = readl_relaxed(mapbase + SA1111_INTPOL0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 for (i = 0; i < 8; i++) {
Russell Kinga5b549e2016-09-07 15:11:14 +0100297 writel_relaxed(ip ^ mask, mapbase + SA1111_INTPOL0);
298 writel_relaxed(ip, mapbase + SA1111_INTPOL0);
299 if (readl_relaxed(mapbase + SA1111_INTSTATCLR0) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 }
302
303 if (i == 8)
Russell King4ed89f22014-10-28 11:26:42 +0000304 pr_err("Danger Will Robinson: failed to re-trigger IRQ%d\n",
305 d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return i == 8 ? -1 : 0;
307}
308
Russell King1629c9a2016-09-06 18:43:28 +0100309static int sa1111_type_irq(struct irq_data *d, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100311 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100312 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
313 u32 ip, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100315 if (flags == IRQ_TYPE_PROBE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100318 if ((!(flags & IRQ_TYPE_EDGE_RISING) ^ !(flags & IRQ_TYPE_EDGE_FALLING)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EINVAL;
320
Russell Kinga5b549e2016-09-07 15:11:14 +0100321 ip = readl_relaxed(mapbase + SA1111_INTPOL0);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100322 if (flags & IRQ_TYPE_EDGE_RISING)
Russell King1629c9a2016-09-06 18:43:28 +0100323 ip &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 else
Russell King1629c9a2016-09-06 18:43:28 +0100325 ip |= mask;
Russell Kinga5b549e2016-09-07 15:11:14 +0100326 writel_relaxed(ip, mapbase + SA1111_INTPOL0);
327 writel_relaxed(ip, mapbase + SA1111_WAKEPOL0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 return 0;
330}
331
Russell King1629c9a2016-09-06 18:43:28 +0100332static int sa1111_wake_irq(struct irq_data *d, unsigned int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100334 struct sa1111 *sachip = irq_data_get_irq_chip_data(d);
Russell King1629c9a2016-09-06 18:43:28 +0100335 void __iomem *mapbase = sachip->base + SA1111_INTC + sa1111_irqbank(d);
336 u32 we, mask = sa1111_irqmask(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Russell Kinga5b549e2016-09-07 15:11:14 +0100338 we = readl_relaxed(mapbase + SA1111_WAKEEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (on)
Russell King1629c9a2016-09-06 18:43:28 +0100340 we |= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 else
Russell King1629c9a2016-09-06 18:43:28 +0100342 we &= ~mask;
Russell Kinga5b549e2016-09-07 15:11:14 +0100343 writel_relaxed(we, mapbase + SA1111_WAKEEN0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return 0;
346}
347
Russell King1629c9a2016-09-06 18:43:28 +0100348static struct irq_chip sa1111_irq_chip = {
349 .name = "SA1111",
Lennert Buytenhek8231e742010-11-29 10:20:06 +0100350 .irq_ack = sa1111_ack_irq,
Russell King1629c9a2016-09-06 18:43:28 +0100351 .irq_mask = sa1111_mask_irq,
352 .irq_unmask = sa1111_unmask_irq,
353 .irq_retrigger = sa1111_retrigger_irq,
354 .irq_set_type = sa1111_type_irq,
355 .irq_set_wake = sa1111_wake_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
Russell King4c9f6d32016-09-07 13:50:27 +0100358static int sa1111_irqdomain_map(struct irq_domain *d, unsigned int irq,
359 irq_hw_number_t hwirq)
360{
361 struct sa1111 *sachip = d->host_data;
362
363 irq_set_chip_data(irq, sachip);
364 irq_set_chip_and_handler(irq, &sa1111_irq_chip, handle_edge_irq);
365 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
366
367 return 0;
368}
369
370static const struct irq_domain_ops sa1111_irqdomain_ops = {
371 .map = sa1111_irqdomain_map,
372 .xlate = irq_domain_xlate_twocell,
373};
374
Russell King36d31212012-01-24 21:25:20 +0000375static int sa1111_setup_irq(struct sa1111 *sachip, unsigned irq_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 void __iomem *irqbase = sachip->base + SA1111_INTC;
Russell King36d31212012-01-24 21:25:20 +0000378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * We're guaranteed that this region hasn't been taken.
382 */
383 request_mem_region(sachip->phys + SA1111_INTC, 512, "irq");
384
Russell King36d31212012-01-24 21:25:20 +0000385 ret = irq_alloc_descs(-1, irq_base, SA1111_IRQ_NR, -1);
386 if (ret <= 0) {
387 dev_err(sachip->dev, "unable to allocate %u irqs: %d\n",
388 SA1111_IRQ_NR, ret);
389 if (ret == 0)
390 ret = -EINVAL;
391 return ret;
392 }
393
394 sachip->irq_base = ret;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* disable all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100397 writel_relaxed(0, irqbase + SA1111_INTEN0);
398 writel_relaxed(0, irqbase + SA1111_INTEN1);
399 writel_relaxed(0, irqbase + SA1111_WAKEEN0);
400 writel_relaxed(0, irqbase + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /*
403 * detect on rising edge. Note: Feb 2001 Errata for SA1111
404 * specifies that S0ReadyInt and S1ReadyInt should be '1'.
405 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100406 writel_relaxed(0, irqbase + SA1111_INTPOL0);
407 writel_relaxed(BIT(IRQ_S0_READY_NINT & 31) |
408 BIT(IRQ_S1_READY_NINT & 31),
409 irqbase + SA1111_INTPOL1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* clear all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100412 writel_relaxed(~0, irqbase + SA1111_INTSTATCLR0);
413 writel_relaxed(~0, irqbase + SA1111_INTSTATCLR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Russell King4c9f6d32016-09-07 13:50:27 +0100415 sachip->irqdomain = irq_domain_add_linear(NULL, SA1111_IRQ_NR,
416 &sa1111_irqdomain_ops,
417 sachip);
418 if (!sachip->irqdomain) {
419 irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
420 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Russell King4c9f6d32016-09-07 13:50:27 +0100423 irq_domain_associate_many(sachip->irqdomain,
424 sachip->irq_base + IRQ_GPAIN0,
425 IRQ_GPAIN0, SSPROR + 1 - IRQ_GPAIN0);
426 irq_domain_associate_many(sachip->irqdomain,
427 sachip->irq_base + AUDXMTDMADONEA,
428 AUDXMTDMADONEA,
429 IRQ_S1_BVD1_STSCHG + 1 - AUDXMTDMADONEA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * Register SA1111 interrupt
433 */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100434 irq_set_irq_type(sachip->irq, IRQ_TYPE_EDGE_RISING);
Russell King056c0ac2015-06-16 23:06:25 +0100435 irq_set_chained_handler_and_data(sachip->irq, sa1111_irq_handler,
436 sachip);
Russell King36d31212012-01-24 21:25:20 +0000437
438 dev_info(sachip->dev, "Providing IRQ%u-%u\n",
439 sachip->irq_base, sachip->irq_base + SA1111_IRQ_NR - 1);
440
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Russell Kingccb7d852016-09-06 16:01:32 +0100444static void sa1111_remove_irq(struct sa1111 *sachip)
445{
446 void __iomem *irqbase = sachip->base + SA1111_INTC;
447
448 /* disable all IRQs */
Russell Kinga5b549e2016-09-07 15:11:14 +0100449 writel_relaxed(0, irqbase + SA1111_INTEN0);
450 writel_relaxed(0, irqbase + SA1111_INTEN1);
451 writel_relaxed(0, irqbase + SA1111_WAKEEN0);
452 writel_relaxed(0, irqbase + SA1111_WAKEEN1);
Russell Kingccb7d852016-09-06 16:01:32 +0100453
Russell King4c9f6d32016-09-07 13:50:27 +0100454 irq_domain_remove(sachip->irqdomain);
Russell Kingccb7d852016-09-06 16:01:32 +0100455
Russell King4c9f6d32016-09-07 13:50:27 +0100456 irq_set_chained_handler_and_data(sachip->irq, NULL, NULL);
457 irq_free_descs(sachip->irq_base, SA1111_IRQ_NR);
458
459 release_mem_region(sachip->phys + SA1111_INTC, 512);
Russell Kingccb7d852016-09-06 16:01:32 +0100460}
461
Russell King17cf5012016-08-31 08:49:45 +0100462enum {
463 SA1111_GPIO_PXDDR = (SA1111_GPIO_PADDR - SA1111_GPIO_PADDR),
464 SA1111_GPIO_PXDRR = (SA1111_GPIO_PADRR - SA1111_GPIO_PADDR),
465 SA1111_GPIO_PXDWR = (SA1111_GPIO_PADWR - SA1111_GPIO_PADDR),
466 SA1111_GPIO_PXSDR = (SA1111_GPIO_PASDR - SA1111_GPIO_PADDR),
467 SA1111_GPIO_PXSSR = (SA1111_GPIO_PASSR - SA1111_GPIO_PADDR),
468};
469
470static struct sa1111 *gc_to_sa1111(struct gpio_chip *gc)
471{
472 return container_of(gc, struct sa1111, gc);
473}
474
475static void __iomem *sa1111_gpio_map_reg(struct sa1111 *sachip, unsigned offset)
476{
477 void __iomem *reg = sachip->base + SA1111_GPIO;
478
479 if (offset < 4)
480 return reg + SA1111_GPIO_PADDR;
481 if (offset < 10)
482 return reg + SA1111_GPIO_PBDDR;
483 if (offset < 18)
484 return reg + SA1111_GPIO_PCDDR;
485 return NULL;
486}
487
488static u32 sa1111_gpio_map_bit(unsigned offset)
489{
490 if (offset < 4)
491 return BIT(offset);
492 if (offset < 10)
493 return BIT(offset - 4);
494 if (offset < 18)
495 return BIT(offset - 10);
496 return 0;
497}
498
499static void sa1111_gpio_modify(void __iomem *reg, u32 mask, u32 set)
500{
501 u32 val;
502
503 val = readl_relaxed(reg);
504 val &= ~mask;
505 val |= mask & set;
506 writel_relaxed(val, reg);
507}
508
509static int sa1111_gpio_get_direction(struct gpio_chip *gc, unsigned offset)
510{
511 struct sa1111 *sachip = gc_to_sa1111(gc);
512 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
513 u32 mask = sa1111_gpio_map_bit(offset);
514
515 return !!(readl_relaxed(reg + SA1111_GPIO_PXDDR) & mask);
516}
517
518static int sa1111_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
519{
520 struct sa1111 *sachip = gc_to_sa1111(gc);
521 unsigned long flags;
522 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
523 u32 mask = sa1111_gpio_map_bit(offset);
524
525 spin_lock_irqsave(&sachip->lock, flags);
526 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, mask);
527 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, mask);
528 spin_unlock_irqrestore(&sachip->lock, flags);
529
530 return 0;
531}
532
533static int sa1111_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
534 int value)
535{
536 struct sa1111 *sachip = gc_to_sa1111(gc);
537 unsigned long flags;
538 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
539 u32 mask = sa1111_gpio_map_bit(offset);
540
541 spin_lock_irqsave(&sachip->lock, flags);
542 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
543 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
544 sa1111_gpio_modify(reg + SA1111_GPIO_PXDDR, mask, 0);
545 sa1111_gpio_modify(reg + SA1111_GPIO_PXSDR, mask, 0);
546 spin_unlock_irqrestore(&sachip->lock, flags);
547
548 return 0;
549}
550
551static int sa1111_gpio_get(struct gpio_chip *gc, unsigned offset)
552{
553 struct sa1111 *sachip = gc_to_sa1111(gc);
554 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
555 u32 mask = sa1111_gpio_map_bit(offset);
556
557 return !!(readl_relaxed(reg + SA1111_GPIO_PXDRR) & mask);
558}
559
560static void sa1111_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
561{
562 struct sa1111 *sachip = gc_to_sa1111(gc);
563 unsigned long flags;
564 void __iomem *reg = sa1111_gpio_map_reg(sachip, offset);
565 u32 mask = sa1111_gpio_map_bit(offset);
566
567 spin_lock_irqsave(&sachip->lock, flags);
568 sa1111_gpio_modify(reg + SA1111_GPIO_PXDWR, mask, value ? mask : 0);
569 sa1111_gpio_modify(reg + SA1111_GPIO_PXSSR, mask, value ? mask : 0);
570 spin_unlock_irqrestore(&sachip->lock, flags);
571}
572
573static void sa1111_gpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
574 unsigned long *bits)
575{
576 struct sa1111 *sachip = gc_to_sa1111(gc);
577 unsigned long flags;
578 void __iomem *reg = sachip->base + SA1111_GPIO;
579 u32 msk, val;
580
581 msk = *mask;
582 val = *bits;
583
584 spin_lock_irqsave(&sachip->lock, flags);
585 sa1111_gpio_modify(reg + SA1111_GPIO_PADWR, msk & 15, val);
586 sa1111_gpio_modify(reg + SA1111_GPIO_PASSR, msk & 15, val);
587 sa1111_gpio_modify(reg + SA1111_GPIO_PBDWR, (msk >> 4) & 255, val >> 4);
588 sa1111_gpio_modify(reg + SA1111_GPIO_PBSSR, (msk >> 4) & 255, val >> 4);
589 sa1111_gpio_modify(reg + SA1111_GPIO_PCDWR, (msk >> 12) & 255, val >> 12);
590 sa1111_gpio_modify(reg + SA1111_GPIO_PCSSR, (msk >> 12) & 255, val >> 12);
591 spin_unlock_irqrestore(&sachip->lock, flags);
592}
593
594static int sa1111_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
595{
596 struct sa1111 *sachip = gc_to_sa1111(gc);
597
598 return sachip->irq_base + offset;
599}
600
601static int sa1111_setup_gpios(struct sa1111 *sachip)
602{
603 sachip->gc.label = "sa1111";
604 sachip->gc.parent = sachip->dev;
605 sachip->gc.owner = THIS_MODULE;
606 sachip->gc.get_direction = sa1111_gpio_get_direction;
607 sachip->gc.direction_input = sa1111_gpio_direction_input;
608 sachip->gc.direction_output = sa1111_gpio_direction_output;
609 sachip->gc.get = sa1111_gpio_get;
610 sachip->gc.set = sa1111_gpio_set;
611 sachip->gc.set_multiple = sa1111_gpio_set_multiple;
612 sachip->gc.to_irq = sa1111_gpio_to_irq;
613 sachip->gc.base = -1;
614 sachip->gc.ngpio = 18;
615
616 return devm_gpiochip_add_data(sachip->dev, &sachip->gc, sachip);
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * Bring the SA1111 out of reset. This requires a set procedure:
621 * 1. nRESET asserted (by hardware)
622 * 2. CLK turned on from SA1110
623 * 3. nRESET deasserted
624 * 4. VCO turned on, PLL_BYPASS turned off
625 * 5. Wait lock time, then assert RCLKEn
626 * 7. PCR set to allow clocking of individual functions
627 *
628 * Until we've done this, the only registers we can access are:
629 * SBI_SKCR
630 * SBI_SMCR
631 * SBI_SKID
632 */
633static void sa1111_wake(struct sa1111 *sachip)
634{
635 unsigned long flags, r;
636
637 spin_lock_irqsave(&sachip->lock, flags);
638
Russell King97d654f2006-03-15 15:54:37 +0000639 clk_enable(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /*
642 * Turn VCO on, and disable PLL Bypass.
643 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100644 r = readl_relaxed(sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 r &= ~SKCR_VCO_OFF;
Russell Kinga5b549e2016-09-07 15:11:14 +0100646 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 r |= SKCR_PLL_BYPASS | SKCR_OE_EN;
Russell Kinga5b549e2016-09-07 15:11:14 +0100648 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /*
651 * Wait lock time. SA1111 manual _doesn't_
652 * specify a figure for this! We choose 100us.
653 */
654 udelay(100);
655
656 /*
657 * Enable RCLK. We also ensure that RDYEN is set.
658 */
659 r |= SKCR_RCLKEN | SKCR_RDYEN;
Russell Kinga5b549e2016-09-07 15:11:14 +0100660 writel_relaxed(r, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Wait 14 RCLK cycles for the chip to finish coming out
664 * of reset. (RCLK=24MHz). This is 590ns.
665 */
666 udelay(1);
667
668 /*
669 * Ensure all clocks are initially off.
670 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100671 writel_relaxed(0, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 spin_unlock_irqrestore(&sachip->lock, flags);
674}
675
676#ifdef CONFIG_ARCH_SA1100
677
678static u32 sa1111_dma_mask[] = {
679 ~0,
680 ~(1 << 20),
681 ~(1 << 23),
682 ~(1 << 24),
683 ~(1 << 25),
684 ~(1 << 20),
685 ~(1 << 20),
686 0,
687};
688
689/*
690 * Configure the SA1111 shared memory controller.
691 */
692void
693sa1111_configure_smc(struct sa1111 *sachip, int sdram, unsigned int drac,
694 unsigned int cas_latency)
695{
696 unsigned int smcr = SMCR_DTIM | SMCR_MBGE | FInsrt(drac, SMCR_DRAC);
697
698 if (cas_latency == 3)
699 smcr |= SMCR_CLAT;
700
Russell Kinga5b549e2016-09-07 15:11:14 +0100701 writel_relaxed(smcr, sachip->base + SA1111_SMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /*
704 * Now clear the bits in the DMA mask to work around the SA1111
705 * DMA erratum (Intel StrongARM SA-1111 Microprocessor Companion
706 * Chip Specification Update, June 2000, Erratum #7).
707 */
708 if (sachip->dev->dma_mask)
709 *sachip->dev->dma_mask &= sa1111_dma_mask[drac >> 2];
710
711 sachip->dev->coherent_dma_mask &= sa1111_dma_mask[drac >> 2];
712}
Russell King0703ed22011-07-04 08:32:21 +0100713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static void sa1111_dev_release(struct device *_dev)
716{
Russell Kingb60752f2016-09-06 13:46:46 +0100717 struct sa1111_dev *dev = to_sa1111_device(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 kfree(dev);
720}
721
722static int
723sa1111_init_one_child(struct sa1111 *sachip, struct resource *parent,
724 struct sa1111_dev_info *info)
725{
726 struct sa1111_dev *dev;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000727 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int ret;
729
Russell Kingd2a02b92006-03-20 19:46:41 +0000730 dev = kzalloc(sizeof(struct sa1111_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!dev) {
732 ret = -ENOMEM;
Russell King924e1d42012-01-29 10:20:00 +0000733 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Russell King924e1d42012-01-29 10:20:00 +0000736 device_initialize(&dev->dev);
Kay Sievers3f978702008-05-30 17:42:11 +0200737 dev_set_name(&dev->dev, "%4.4lx", info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 dev->devid = info->devid;
739 dev->dev.parent = sachip->dev;
740 dev->dev.bus = &sa1111_bus_type;
741 dev->dev.release = sa1111_dev_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev->res.start = sachip->phys + info->offset;
743 dev->res.end = dev->res.start + 511;
Kay Sievers3f978702008-05-30 17:42:11 +0200744 dev->res.name = dev_name(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 dev->res.flags = IORESOURCE_MEM;
746 dev->mapbase = sachip->base + info->offset;
747 dev->skpcr_mask = info->skpcr_mask;
Russell Kingf03ecaa2012-01-16 00:09:22 +0000748
749 for (i = 0; i < ARRAY_SIZE(info->irq); i++)
750 dev->irq[i] = sachip->irq_base + info->irq[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Russell King09a2ba22012-01-29 09:31:31 +0000752 /*
Russell King21d1c772012-01-29 10:22:49 +0000753 * If the parent device has a DMA mask associated with it, and
754 * this child supports DMA, propagate it down to the children.
Russell King09a2ba22012-01-29 09:31:31 +0000755 */
Russell King21d1c772012-01-29 10:22:49 +0000756 if (info->dma && sachip->dev->dma_mask) {
Russell King09a2ba22012-01-29 09:31:31 +0000757 dev->dma_mask = *sachip->dev->dma_mask;
758 dev->dev.dma_mask = &dev->dma_mask;
759 dev->dev.coherent_dma_mask = sachip->dev->coherent_dma_mask;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret = request_resource(parent, &dev->res);
763 if (ret) {
Russell King22eeaff2012-01-29 10:28:09 +0000764 dev_err(sachip->dev, "failed to allocate resource for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 dev->res.name);
Russell King924e1d42012-01-29 10:20:00 +0000766 goto err_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Russell King924e1d42012-01-29 10:20:00 +0000769 ret = device_add(&dev->dev);
770 if (ret)
771 goto err_add;
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Russell King924e1d42012-01-29 10:20:00 +0000774 err_add:
775 release_resource(&dev->res);
776 err_resource:
777 put_device(&dev->dev);
778 err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return ret;
780}
781
782/**
783 * sa1111_probe - probe for a single SA1111 chip.
784 * @phys_addr: physical address of device.
785 *
786 * Probe for a SA1111 chip. This must be called
787 * before any other SA1111-specific code.
788 *
789 * Returns:
790 * %-ENODEV device not found.
791 * %-EBUSY physical address already marked in-use.
Russell Kingf03ecaa2012-01-16 00:09:22 +0000792 * %-EINVAL no platform data passed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * %0 successful.
794 */
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800795static int __sa1111_probe(struct device *me, struct resource *mem, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Russell Kingf03ecaa2012-01-16 00:09:22 +0000797 struct sa1111_platform_data *pd = me->platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct sa1111 *sachip;
799 unsigned long id;
David Brownell416112f2006-08-27 13:09:14 +0100800 unsigned int has_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int i, ret = -ENODEV;
802
Russell Kingf03ecaa2012-01-16 00:09:22 +0000803 if (!pd)
804 return -EINVAL;
805
Russell King7d53c1f2016-09-06 15:29:26 +0100806 sachip = devm_kzalloc(me, sizeof(struct sa1111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!sachip)
808 return -ENOMEM;
809
Russell Kingdeee8562016-08-30 22:47:35 +0100810 sachip->clk = devm_clk_get(me, "SA1111_CLK");
811 if (IS_ERR(sachip->clk))
812 return PTR_ERR(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000813
Russell King72ae00c2011-09-22 11:41:07 +0100814 ret = clk_prepare(sachip->clk);
815 if (ret)
Russell Kingdeee8562016-08-30 22:47:35 +0100816 return ret;
Russell King72ae00c2011-09-22 11:41:07 +0100817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_lock_init(&sachip->lock);
819
820 sachip->dev = me;
821 dev_set_drvdata(sachip->dev, sachip);
822
Russell Kingae99ddb2012-01-26 13:25:47 +0000823 sachip->pdata = pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 sachip->phys = mem->start;
825 sachip->irq = irq;
826
827 /*
828 * Map the whole region. This also maps the
829 * registers for our children.
830 */
831 sachip->base = ioremap(mem->start, PAGE_SIZE * 2);
832 if (!sachip->base) {
833 ret = -ENOMEM;
Russell King72ae00c2011-09-22 11:41:07 +0100834 goto err_clk_unprep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 /*
838 * Probe for the chip. Only touch the SBI registers.
839 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100840 id = readl_relaxed(sachip->base + SA1111_SKID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
842 printk(KERN_DEBUG "SA1111 not detected: ID = %08lx\n", id);
843 ret = -ENODEV;
Russell King97d654f2006-03-15 15:54:37 +0000844 goto err_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Russell King4ed89f22014-10-28 11:26:42 +0000847 pr_info("SA1111 Microprocessor Companion Chip: silicon revision %lx, metal revision %lx\n",
848 (id & SKID_SIREV_MASK) >> 4, id & SKID_MTREV_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
851 * We found it. Wake the chip up, and initialise.
852 */
853 sa1111_wake(sachip);
854
Russell King36d31212012-01-24 21:25:20 +0000855 /*
856 * The interrupt controller must be initialised before any
857 * other device to ensure that the interrupts are available.
858 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100859 ret = sa1111_setup_irq(sachip, pd->irq_base);
860 if (ret)
861 goto err_clk;
Russell King36d31212012-01-24 21:25:20 +0000862
Russell King17cf5012016-08-31 08:49:45 +0100863 /* Setup the GPIOs - should really be done after the IRQ setup */
864 ret = sa1111_setup_gpios(sachip);
865 if (ret)
866 goto err_irq;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868#ifdef CONFIG_ARCH_SA1100
David Brownell416112f2006-08-27 13:09:14 +0100869 {
870 unsigned int val;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * The SDRAM configuration of the SA1110 and the SA1111 must
874 * match. This is very important to ensure that SA1111 accesses
875 * don't corrupt the SDRAM. Note that this ungates the SA1111's
876 * MBGNT signal, so we must have called sa1110_mb_disable()
877 * beforehand.
878 */
879 sa1111_configure_smc(sachip, 1,
880 FExtr(MDCNFG, MDCNFG_SA1110_DRAC0),
881 FExtr(MDCNFG, MDCNFG_SA1110_TDL0));
882
883 /*
884 * We only need to turn on DCLK whenever we want to use the
885 * DMA. It can otherwise be held firmly in the off position.
886 * (currently, we always enable it.)
887 */
Russell Kinga5b549e2016-09-07 15:11:14 +0100888 val = readl_relaxed(sachip->base + SA1111_SKPCR);
889 writel_relaxed(val | SKPCR_DCLKEN, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Enable the SA1110 memory bus request and grant signals.
893 */
894 sa1110_mb_enable();
David Brownell416112f2006-08-27 13:09:14 +0100895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#endif
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 g_sa1111 = sachip;
899
900 has_devs = ~0;
Russell King07be45f2012-01-26 13:34:21 +0000901 if (pd)
902 has_devs &= ~pd->disable_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 for (i = 0; i < ARRAY_SIZE(sa1111_devices); i++)
Russell Kinge5c0fc42012-01-25 10:42:52 +0000905 if (sa1111_devices[i].devid & has_devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 sa1111_init_one_child(sachip, mem, &sa1111_devices[i]);
907
908 return 0;
909
Russell King17cf5012016-08-31 08:49:45 +0100910 err_irq:
911 sa1111_remove_irq(sachip);
Russell King87d5dd62016-09-06 16:09:17 +0100912 err_clk:
913 clk_disable(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000914 err_unmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 iounmap(sachip->base);
Russell King72ae00c2011-09-22 11:41:07 +0100916 err_clk_unprep:
917 clk_unprepare(sachip->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return ret;
919}
920
Russell King522c37b2005-06-22 09:52:26 +0100921static int sa1111_remove_one(struct device *dev, void *data)
922{
Russell Kingb60752f2016-09-06 13:46:46 +0100923 struct sa1111_dev *sadev = to_sa1111_device(dev);
Russell Kingeac8dbf2016-09-03 09:14:58 +0100924 if (dev->bus != &sa1111_bus_type)
925 return 0;
Russell King924e1d42012-01-29 10:20:00 +0000926 device_del(&sadev->dev);
927 release_resource(&sadev->res);
928 put_device(&sadev->dev);
Russell King522c37b2005-06-22 09:52:26 +0100929 return 0;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static void __sa1111_remove(struct sa1111 *sachip)
933{
Russell King522c37b2005-06-22 09:52:26 +0100934 device_for_each_child(sachip->dev, NULL, sa1111_remove_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Russell Kingccb7d852016-09-06 16:01:32 +0100936 sa1111_remove_irq(sachip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Russell King97d654f2006-03-15 15:54:37 +0000938 clk_disable(sachip->clk);
Russell King72ae00c2011-09-22 11:41:07 +0100939 clk_unprepare(sachip->clk);
Russell King97d654f2006-03-15 15:54:37 +0000940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 iounmap(sachip->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944struct sa1111_save_data {
945 unsigned int skcr;
946 unsigned int skpcr;
947 unsigned int skcdr;
948 unsigned char skaud;
949 unsigned char skpwm0;
950 unsigned char skpwm1;
951
952 /*
953 * Interrupt controller
954 */
955 unsigned int intpol0;
956 unsigned int intpol1;
957 unsigned int inten0;
958 unsigned int inten1;
959 unsigned int wakepol0;
960 unsigned int wakepol1;
961 unsigned int wakeen0;
962 unsigned int wakeen1;
963};
964
965#ifdef CONFIG_PM
966
Russell King06dfe5c2016-09-06 14:34:05 +0100967static int sa1111_suspend_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Russell King06dfe5c2016-09-06 14:34:05 +0100969 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct sa1111_save_data *save;
971 unsigned long flags;
972 unsigned int val;
973 void __iomem *base;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 save = kmalloc(sizeof(struct sa1111_save_data), GFP_KERNEL);
976 if (!save)
977 return -ENOMEM;
Rafael J. Wysocki93160c62007-07-09 11:39:19 -0700978 sachip->saved_state = save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 spin_lock_irqsave(&sachip->lock, flags);
981
982 /*
983 * Save state.
984 */
985 base = sachip->base;
Russell Kinga5b549e2016-09-07 15:11:14 +0100986 save->skcr = readl_relaxed(base + SA1111_SKCR);
987 save->skpcr = readl_relaxed(base + SA1111_SKPCR);
988 save->skcdr = readl_relaxed(base + SA1111_SKCDR);
989 save->skaud = readl_relaxed(base + SA1111_SKAUD);
990 save->skpwm0 = readl_relaxed(base + SA1111_SKPWM0);
991 save->skpwm1 = readl_relaxed(base + SA1111_SKPWM1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Russell Kinga5b549e2016-09-07 15:11:14 +0100993 writel_relaxed(0, sachip->base + SA1111_SKPWM0);
994 writel_relaxed(0, sachip->base + SA1111_SKPWM1);
Russell Kinga22db0f32012-02-03 19:05:13 +0000995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 base = sachip->base + SA1111_INTC;
Russell Kinga5b549e2016-09-07 15:11:14 +0100997 save->intpol0 = readl_relaxed(base + SA1111_INTPOL0);
998 save->intpol1 = readl_relaxed(base + SA1111_INTPOL1);
999 save->inten0 = readl_relaxed(base + SA1111_INTEN0);
1000 save->inten1 = readl_relaxed(base + SA1111_INTEN1);
1001 save->wakepol0 = readl_relaxed(base + SA1111_WAKEPOL0);
1002 save->wakepol1 = readl_relaxed(base + SA1111_WAKEPOL1);
1003 save->wakeen0 = readl_relaxed(base + SA1111_WAKEEN0);
1004 save->wakeen1 = readl_relaxed(base + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
1007 * Disable.
1008 */
Russell Kinga5b549e2016-09-07 15:11:14 +01001009 val = readl_relaxed(sachip->base + SA1111_SKCR);
1010 writel_relaxed(val | SKCR_SLEEP, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Russell King97d654f2006-03-15 15:54:37 +00001012 clk_disable(sachip->clk);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 spin_unlock_irqrestore(&sachip->lock, flags);
1015
Russell King29c140b2012-02-03 19:03:31 +00001016#ifdef CONFIG_ARCH_SA1100
1017 sa1110_mb_disable();
1018#endif
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
1021}
1022
1023/*
1024 * sa1111_resume - Restore the SA1111 device state.
1025 * @dev: device to restore
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 *
1027 * Restore the general state of the SA1111; clock control and
1028 * interrupt controller. Other parts of the SA1111 must be
1029 * restored by their respective drivers, and must be called
1030 * via LDM after this function.
1031 */
Russell King06dfe5c2016-09-06 14:34:05 +01001032static int sa1111_resume_noirq(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Russell King06dfe5c2016-09-06 14:34:05 +01001034 struct sa1111 *sachip = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct sa1111_save_data *save;
1036 unsigned long flags, id;
1037 void __iomem *base;
1038
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001039 save = sachip->saved_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!save)
1041 return 0;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Ensure that the SA1111 is still here.
1045 * FIXME: shouldn't do this here.
1046 */
Russell Kinga5b549e2016-09-07 15:11:14 +01001047 id = readl_relaxed(sachip->base + SA1111_SKID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if ((id & SKID_ID_MASK) != SKID_SA1111_ID) {
1049 __sa1111_remove(sachip);
Russell King06dfe5c2016-09-06 14:34:05 +01001050 dev_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 kfree(save);
1052 return 0;
1053 }
1054
1055 /*
1056 * First of all, wake up the chip.
1057 */
1058 sa1111_wake(sachip);
Marek Vašut3defb242010-05-26 23:53:09 +01001059
Russell King29c140b2012-02-03 19:03:31 +00001060#ifdef CONFIG_ARCH_SA1100
1061 /* Enable the memory bus request/grant signals */
1062 sa1110_mb_enable();
1063#endif
1064
Marek Vašut3defb242010-05-26 23:53:09 +01001065 /*
1066 * Only lock for write ops. Also, sa1111_wake must be called with
1067 * released spinlock!
1068 */
1069 spin_lock_irqsave(&sachip->lock, flags);
1070
Russell Kinga5b549e2016-09-07 15:11:14 +01001071 writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN0);
1072 writel_relaxed(0, sachip->base + SA1111_INTC + SA1111_INTEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 base = sachip->base;
Russell Kinga5b549e2016-09-07 15:11:14 +01001075 writel_relaxed(save->skcr, base + SA1111_SKCR);
1076 writel_relaxed(save->skpcr, base + SA1111_SKPCR);
1077 writel_relaxed(save->skcdr, base + SA1111_SKCDR);
1078 writel_relaxed(save->skaud, base + SA1111_SKAUD);
1079 writel_relaxed(save->skpwm0, base + SA1111_SKPWM0);
1080 writel_relaxed(save->skpwm1, base + SA1111_SKPWM1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 base = sachip->base + SA1111_INTC;
Russell Kinga5b549e2016-09-07 15:11:14 +01001083 writel_relaxed(save->intpol0, base + SA1111_INTPOL0);
1084 writel_relaxed(save->intpol1, base + SA1111_INTPOL1);
1085 writel_relaxed(save->inten0, base + SA1111_INTEN0);
1086 writel_relaxed(save->inten1, base + SA1111_INTEN1);
1087 writel_relaxed(save->wakepol0, base + SA1111_WAKEPOL0);
1088 writel_relaxed(save->wakepol1, base + SA1111_WAKEPOL1);
1089 writel_relaxed(save->wakeen0, base + SA1111_WAKEEN0);
1090 writel_relaxed(save->wakeen1, base + SA1111_WAKEEN1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 spin_unlock_irqrestore(&sachip->lock, flags);
1093
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001094 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 kfree(save);
1096
1097 return 0;
1098}
1099
1100#else
Russell King06dfe5c2016-09-06 14:34:05 +01001101#define sa1111_suspend_noirq NULL
1102#define sa1111_resume_noirq NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103#endif
1104
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001105static int sa1111_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct resource *mem;
1108 int irq;
1109
1110 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1111 if (!mem)
1112 return -EINVAL;
1113 irq = platform_get_irq(pdev, 0);
David Vrabel48944732006-01-19 17:56:29 +00001114 if (irq < 0)
Russell Kingcb034402016-09-03 10:21:51 +01001115 return irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Russell King3ae5eae2005-11-09 22:32:44 +00001117 return __sa1111_probe(&pdev->dev, mem, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Russell King3ae5eae2005-11-09 22:32:44 +00001120static int sa1111_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Russell King3ae5eae2005-11-09 22:32:44 +00001122 struct sa1111 *sachip = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (sachip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#ifdef CONFIG_PM
Rafael J. Wysocki93160c62007-07-09 11:39:19 -07001126 kfree(sachip->saved_state);
1127 sachip->saved_state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
Julia Lawallf2d24202010-07-30 17:17:28 +02001129 __sa1111_remove(sachip);
1130 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
1133 return 0;
1134}
1135
Russell King06dfe5c2016-09-06 14:34:05 +01001136static struct dev_pm_ops sa1111_pm_ops = {
1137 .suspend_noirq = sa1111_suspend_noirq,
1138 .resume_noirq = sa1111_resume_noirq,
1139};
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
1142 * Not sure if this should be on the system bus or not yet.
1143 * We really want some way to register a system device at
1144 * the per-machine level, and then have this driver pick
1145 * up the registered devices.
1146 *
1147 * We also need to handle the SDRAM configuration for
1148 * PXA250/SA1110 machine classes.
1149 */
Russell King3ae5eae2005-11-09 22:32:44 +00001150static struct platform_driver sa1111_device_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 .probe = sa1111_probe,
1152 .remove = sa1111_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001153 .driver = {
1154 .name = "sa1111",
Russell King06dfe5c2016-09-06 14:34:05 +01001155 .pm = &sa1111_pm_ops,
Russell King3ae5eae2005-11-09 22:32:44 +00001156 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157};
1158
1159/*
1160 * Get the parent device driver (us) structure
1161 * from a child function device
1162 */
1163static inline struct sa1111 *sa1111_chip_driver(struct sa1111_dev *sadev)
1164{
1165 return (struct sa1111 *)dev_get_drvdata(sadev->dev.parent);
1166}
1167
1168/*
1169 * The bits in the opdiv field are non-linear.
1170 */
1171static unsigned char opdiv_table[] = { 1, 4, 2, 8 };
1172
1173static unsigned int __sa1111_pll_clock(struct sa1111 *sachip)
1174{
1175 unsigned int skcdr, fbdiv, ipdiv, opdiv;
1176
Russell Kinga5b549e2016-09-07 15:11:14 +01001177 skcdr = readl_relaxed(sachip->base + SA1111_SKCDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 fbdiv = (skcdr & 0x007f) + 2;
1180 ipdiv = ((skcdr & 0x0f80) >> 7) + 2;
1181 opdiv = opdiv_table[(skcdr & 0x3000) >> 12];
1182
1183 return 3686400 * fbdiv / (ipdiv * opdiv);
1184}
1185
1186/**
1187 * sa1111_pll_clock - return the current PLL clock frequency.
1188 * @sadev: SA1111 function block
1189 *
1190 * BUG: we should look at SKCR. We also blindly believe that
1191 * the chip is being fed with the 3.6864MHz clock.
1192 *
1193 * Returns the PLL clock in Hz.
1194 */
1195unsigned int sa1111_pll_clock(struct sa1111_dev *sadev)
1196{
1197 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1198
1199 return __sa1111_pll_clock(sachip);
1200}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001201EXPORT_SYMBOL(sa1111_pll_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203/**
1204 * sa1111_select_audio_mode - select I2S or AC link mode
1205 * @sadev: SA1111 function block
1206 * @mode: One of %SA1111_AUDIO_ACLINK or %SA1111_AUDIO_I2S
1207 *
1208 * Frob the SKCR to select AC Link mode or I2S mode for
1209 * the audio block.
1210 */
1211void sa1111_select_audio_mode(struct sa1111_dev *sadev, int mode)
1212{
1213 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1214 unsigned long flags;
1215 unsigned int val;
1216
1217 spin_lock_irqsave(&sachip->lock, flags);
1218
Russell Kinga5b549e2016-09-07 15:11:14 +01001219 val = readl_relaxed(sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (mode == SA1111_AUDIO_I2S) {
1221 val &= ~SKCR_SELAC;
1222 } else {
1223 val |= SKCR_SELAC;
1224 }
Russell Kinga5b549e2016-09-07 15:11:14 +01001225 writel_relaxed(val, sachip->base + SA1111_SKCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 spin_unlock_irqrestore(&sachip->lock, flags);
1228}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001229EXPORT_SYMBOL(sa1111_select_audio_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231/**
1232 * sa1111_set_audio_rate - set the audio sample rate
1233 * @sadev: SA1111 SAC function block
1234 * @rate: sample rate to select
1235 */
1236int sa1111_set_audio_rate(struct sa1111_dev *sadev, int rate)
1237{
1238 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1239 unsigned int div;
1240
1241 if (sadev->devid != SA1111_DEVID_SAC)
1242 return -EINVAL;
1243
1244 div = (__sa1111_pll_clock(sachip) / 256 + rate / 2) / rate;
1245 if (div == 0)
1246 div = 1;
1247 if (div > 128)
1248 div = 128;
1249
Russell Kinga5b549e2016-09-07 15:11:14 +01001250 writel_relaxed(div - 1, sachip->base + SA1111_SKAUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 return 0;
1253}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001254EXPORT_SYMBOL(sa1111_set_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256/**
1257 * sa1111_get_audio_rate - get the audio sample rate
1258 * @sadev: SA1111 SAC function block device
1259 */
1260int sa1111_get_audio_rate(struct sa1111_dev *sadev)
1261{
1262 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1263 unsigned long div;
1264
1265 if (sadev->devid != SA1111_DEVID_SAC)
1266 return -EINVAL;
1267
Russell Kinga5b549e2016-09-07 15:11:14 +01001268 div = readl_relaxed(sachip->base + SA1111_SKAUD) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 return __sa1111_pll_clock(sachip) / (256 * div);
1271}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001272EXPORT_SYMBOL(sa1111_get_audio_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274void sa1111_set_io_dir(struct sa1111_dev *sadev,
1275 unsigned int bits, unsigned int dir,
1276 unsigned int sleep_dir)
1277{
1278 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1279 unsigned long flags;
1280 unsigned int val;
1281 void __iomem *gpio = sachip->base + SA1111_GPIO;
1282
1283#define MODIFY_BITS(port, mask, dir) \
1284 if (mask) { \
Russell Kinga5b549e2016-09-07 15:11:14 +01001285 val = readl_relaxed(port); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 val &= ~(mask); \
1287 val |= (dir) & (mask); \
Russell Kinga5b549e2016-09-07 15:11:14 +01001288 writel_relaxed(val, port); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 spin_lock_irqsave(&sachip->lock, flags);
1292 MODIFY_BITS(gpio + SA1111_GPIO_PADDR, bits & 15, dir);
1293 MODIFY_BITS(gpio + SA1111_GPIO_PBDDR, (bits >> 8) & 255, dir >> 8);
1294 MODIFY_BITS(gpio + SA1111_GPIO_PCDDR, (bits >> 16) & 255, dir >> 16);
1295
1296 MODIFY_BITS(gpio + SA1111_GPIO_PASDR, bits & 15, sleep_dir);
1297 MODIFY_BITS(gpio + SA1111_GPIO_PBSDR, (bits >> 8) & 255, sleep_dir >> 8);
1298 MODIFY_BITS(gpio + SA1111_GPIO_PCSDR, (bits >> 16) & 255, sleep_dir >> 16);
1299 spin_unlock_irqrestore(&sachip->lock, flags);
1300}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001301EXPORT_SYMBOL(sa1111_set_io_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303void sa1111_set_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1304{
1305 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1306 unsigned long flags;
1307 unsigned int val;
1308 void __iomem *gpio = sachip->base + SA1111_GPIO;
1309
1310 spin_lock_irqsave(&sachip->lock, flags);
1311 MODIFY_BITS(gpio + SA1111_GPIO_PADWR, bits & 15, v);
1312 MODIFY_BITS(gpio + SA1111_GPIO_PBDWR, (bits >> 8) & 255, v >> 8);
1313 MODIFY_BITS(gpio + SA1111_GPIO_PCDWR, (bits >> 16) & 255, v >> 16);
1314 spin_unlock_irqrestore(&sachip->lock, flags);
1315}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001316EXPORT_SYMBOL(sa1111_set_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318void sa1111_set_sleep_io(struct sa1111_dev *sadev, unsigned int bits, unsigned int v)
1319{
1320 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1321 unsigned long flags;
1322 unsigned int val;
1323 void __iomem *gpio = sachip->base + SA1111_GPIO;
1324
1325 spin_lock_irqsave(&sachip->lock, flags);
1326 MODIFY_BITS(gpio + SA1111_GPIO_PASSR, bits & 15, v);
1327 MODIFY_BITS(gpio + SA1111_GPIO_PBSSR, (bits >> 8) & 255, v >> 8);
1328 MODIFY_BITS(gpio + SA1111_GPIO_PCSSR, (bits >> 16) & 255, v >> 16);
1329 spin_unlock_irqrestore(&sachip->lock, flags);
1330}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001331EXPORT_SYMBOL(sa1111_set_sleep_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333/*
1334 * Individual device operations.
1335 */
1336
1337/**
1338 * sa1111_enable_device - enable an on-chip SA1111 function block
1339 * @sadev: SA1111 function block device to enable
1340 */
Russell Kingae99ddb2012-01-26 13:25:47 +00001341int sa1111_enable_device(struct sa1111_dev *sadev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1344 unsigned long flags;
1345 unsigned int val;
Russell Kingae99ddb2012-01-26 13:25:47 +00001346 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Russell Kingae99ddb2012-01-26 13:25:47 +00001348 if (sachip->pdata && sachip->pdata->enable)
1349 ret = sachip->pdata->enable(sachip->pdata->data, sadev->devid);
1350
1351 if (ret == 0) {
1352 spin_lock_irqsave(&sachip->lock, flags);
Russell Kinga5b549e2016-09-07 15:11:14 +01001353 val = readl_relaxed(sachip->base + SA1111_SKPCR);
1354 writel_relaxed(val | sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
Russell Kingae99ddb2012-01-26 13:25:47 +00001355 spin_unlock_irqrestore(&sachip->lock, flags);
1356 }
1357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001359EXPORT_SYMBOL(sa1111_enable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361/**
1362 * sa1111_disable_device - disable an on-chip SA1111 function block
1363 * @sadev: SA1111 function block device to disable
1364 */
1365void sa1111_disable_device(struct sa1111_dev *sadev)
1366{
1367 struct sa1111 *sachip = sa1111_chip_driver(sadev);
1368 unsigned long flags;
1369 unsigned int val;
1370
1371 spin_lock_irqsave(&sachip->lock, flags);
Russell Kinga5b549e2016-09-07 15:11:14 +01001372 val = readl_relaxed(sachip->base + SA1111_SKPCR);
1373 writel_relaxed(val & ~sadev->skpcr_mask, sachip->base + SA1111_SKPCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_unlock_irqrestore(&sachip->lock, flags);
Russell Kingae99ddb2012-01-26 13:25:47 +00001375
1376 if (sachip->pdata && sachip->pdata->disable)
1377 sachip->pdata->disable(sachip->pdata->data, sadev->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001379EXPORT_SYMBOL(sa1111_disable_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Russell Kingcf6e4ca2016-09-06 00:45:33 +01001381int sa1111_get_irq(struct sa1111_dev *sadev, unsigned num)
1382{
1383 if (num >= ARRAY_SIZE(sadev->irq))
1384 return -EINVAL;
1385 return sadev->irq[num];
1386}
1387EXPORT_SYMBOL_GPL(sa1111_get_irq);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
1390 * SA1111 "Register Access Bus."
1391 *
1392 * We model this as a regular bus type, and hang devices directly
1393 * off this.
1394 */
1395static int sa1111_match(struct device *_dev, struct device_driver *_drv)
1396{
Russell Kingb60752f2016-09-06 13:46:46 +01001397 struct sa1111_dev *dev = to_sa1111_device(_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct sa1111_driver *drv = SA1111_DRV(_drv);
1399
Marek Szyprowski17f29d362016-02-15 09:24:05 +01001400 return !!(dev->devid & drv->devid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static int sa1111_bus_probe(struct device *dev)
1404{
Russell Kingb60752f2016-09-06 13:46:46 +01001405 struct sa1111_dev *sadev = to_sa1111_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1407 int ret = -ENODEV;
1408
1409 if (drv->probe)
1410 ret = drv->probe(sadev);
1411 return ret;
1412}
1413
1414static int sa1111_bus_remove(struct device *dev)
1415{
Russell Kingb60752f2016-09-06 13:46:46 +01001416 struct sa1111_dev *sadev = to_sa1111_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct sa1111_driver *drv = SA1111_DRV(dev->driver);
1418 int ret = 0;
1419
1420 if (drv->remove)
1421 ret = drv->remove(sadev);
1422 return ret;
1423}
1424
1425struct bus_type sa1111_bus_type = {
1426 .name = "sa1111-rab",
1427 .match = sa1111_match,
Russell King2876ba42006-01-05 14:32:32 +00001428 .probe = sa1111_bus_probe,
1429 .remove = sa1111_bus_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430};
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001431EXPORT_SYMBOL(sa1111_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433int sa1111_driver_register(struct sa1111_driver *driver)
1434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 driver->drv.bus = &sa1111_bus_type;
1436 return driver_register(&driver->drv);
1437}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001438EXPORT_SYMBOL(sa1111_driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440void sa1111_driver_unregister(struct sa1111_driver *driver)
1441{
1442 driver_unregister(&driver->drv);
1443}
Hartley Sweeten0a4bc5e2009-09-29 23:49:02 +01001444EXPORT_SYMBOL(sa1111_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Russell King09a2ba22012-01-29 09:31:31 +00001446#ifdef CONFIG_DMABOUNCE
1447/*
1448 * According to the "Intel StrongARM SA-1111 Microprocessor Companion
1449 * Chip Specification Update" (June 2000), erratum #7, there is a
1450 * significant bug in the SA1111 SDRAM shared memory controller. If
1451 * an access to a region of memory above 1MB relative to the bank base,
1452 * it is important that address bit 10 _NOT_ be asserted. Depending
1453 * on the configuration of the RAM, bit 10 may correspond to one
1454 * of several different (processor-relative) address bits.
1455 *
1456 * This routine only identifies whether or not a given DMA address
1457 * is susceptible to the bug.
1458 *
1459 * This should only get called for sa1111_device types due to the
1460 * way we configure our device dma_masks.
1461 */
1462static int sa1111_needs_bounce(struct device *dev, dma_addr_t addr, size_t size)
1463{
1464 /*
1465 * Section 4.6 of the "Intel StrongARM SA-1111 Development Module
1466 * User's Guide" mentions that jumpers R51 and R52 control the
1467 * target of SA-1111 DMA (either SDRAM bank 0 on Assabet, or
1468 * SDRAM bank 1 on Neponset). The default configuration selects
1469 * Assabet, so any address in bank 1 is necessarily invalid.
1470 */
1471 return (machine_is_assabet() || machine_is_pfs168()) &&
1472 (addr >= 0xc8000000 || (addr + size) >= 0xc8000000);
1473}
1474
1475static int sa1111_notifier_call(struct notifier_block *n, unsigned long action,
1476 void *data)
1477{
Russell Kingb60752f2016-09-06 13:46:46 +01001478 struct sa1111_dev *dev = to_sa1111_device(data);
Russell King09a2ba22012-01-29 09:31:31 +00001479
1480 switch (action) {
1481 case BUS_NOTIFY_ADD_DEVICE:
1482 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL) {
1483 int ret = dmabounce_register_dev(&dev->dev, 1024, 4096,
1484 sa1111_needs_bounce);
1485 if (ret)
1486 dev_err(&dev->dev, "failed to register with dmabounce: %d\n", ret);
1487 }
1488 break;
1489
1490 case BUS_NOTIFY_DEL_DEVICE:
1491 if (dev->dev.dma_mask && dev->dma_mask < 0xffffffffUL)
1492 dmabounce_unregister_dev(&dev->dev);
1493 break;
1494 }
1495 return NOTIFY_OK;
1496}
1497
1498static struct notifier_block sa1111_bus_notifier = {
1499 .notifier_call = sa1111_notifier_call,
1500};
1501#endif
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static int __init sa1111_init(void)
1504{
1505 int ret = bus_register(&sa1111_bus_type);
Russell King09a2ba22012-01-29 09:31:31 +00001506#ifdef CONFIG_DMABOUNCE
1507 if (ret == 0)
1508 bus_register_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1509#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (ret == 0)
Russell King3ae5eae2005-11-09 22:32:44 +00001511 platform_driver_register(&sa1111_device_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return ret;
1513}
1514
1515static void __exit sa1111_exit(void)
1516{
Russell King3ae5eae2005-11-09 22:32:44 +00001517 platform_driver_unregister(&sa1111_device_driver);
Russell King09a2ba22012-01-29 09:31:31 +00001518#ifdef CONFIG_DMABOUNCE
1519 bus_unregister_notifier(&sa1111_bus_type, &sa1111_bus_notifier);
1520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 bus_unregister(&sa1111_bus_type);
1522}
1523
Russell King72724382005-11-15 19:04:22 +00001524subsys_initcall(sa1111_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525module_exit(sa1111_exit);
1526
1527MODULE_DESCRIPTION("Intel Corporation SA1111 core driver");
1528MODULE_LICENSE("GPL");