blob: 5cac2c540f4f86b6d73ecd5b24617e4ee9b27705 [file] [log] [blame]
Jason Wangfa94f8d2010-06-24 21:11:28 +08001/*
2 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2010 Jason Wang <jason77.wang@gmail.com>
4 *
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
8 *
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
11 */
12
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/io.h>
16#include <linux/platform_device.h>
17#include <linux/gpio.h>
18#include <linux/smsc911x.h>
Sascha Hauer33499df2012-03-03 12:40:04 +010019#include <linux/regulator/machine.h>
20#include <linux/regulator/fixed.h>
Jason Wangfa94f8d2010-06-24 21:11:28 +080021
22#include <mach/hardware.h>
23
24/* LAN9217 ethernet base address */
25#define LAN9217_BASE_ADDR(n) (n + 0x0)
26/* External UART */
27#define UARTA_BASE_ADDR(n) (n + 0x8000)
28#define UARTB_BASE_ADDR(n) (n + 0x10000)
29
30#define BOARD_IO_ADDR(n) (n + 0x20000)
31/* LED switchs */
32#define LED_SWITCH_REG 0x00
33/* buttons */
34#define SWITCH_BUTTONS_REG 0x08
35/* status, interrupt */
36#define INTR_STATUS_REG 0x10
37#define INTR_MASK_REG 0x38
38#define INTR_RESET_REG 0x20
39/* magic word for debug CPLD */
40#define MAGIC_NUMBER1_REG 0x40
41#define MAGIC_NUMBER2_REG 0x48
42/* CPLD code version */
43#define CPLD_CODE_VER_REG 0x50
44/* magic word for debug CPLD */
45#define MAGIC_NUMBER3_REG 0x58
46/* module reset register*/
47#define MODULE_RESET_REG 0x60
48/* CPU ID and Personality ID */
49#define MCU_BOARD_ID_REG 0x68
50
51#define MXC_IRQ_TO_EXPIO(irq) ((irq) - MXC_BOARD_IRQ_START)
52#define MXC_IRQ_TO_GPIO(irq) ((irq) - MXC_INTERNAL_IRQS)
53
54#define MXC_EXP_IO_BASE (MXC_BOARD_IRQ_START)
55#define MXC_MAX_EXP_IO_LINES 16
56
57/* interrupts like external uart , external ethernet etc*/
58#define EXPIO_INT_ENET (MXC_BOARD_IRQ_START + 0)
59#define EXPIO_INT_XUART_A (MXC_BOARD_IRQ_START + 1)
60#define EXPIO_INT_XUART_B (MXC_BOARD_IRQ_START + 2)
61#define EXPIO_INT_BUTTON_A (MXC_BOARD_IRQ_START + 3)
62#define EXPIO_INT_BUTTON_B (MXC_BOARD_IRQ_START + 4)
63
64static void __iomem *brd_io;
Jason Wangfa94f8d2010-06-24 21:11:28 +080065
66static struct resource smsc911x_resources[] = {
67 {
68 .flags = IORESOURCE_MEM,
69 } , {
70 .start = EXPIO_INT_ENET,
71 .end = EXPIO_INT_ENET,
72 .flags = IORESOURCE_IRQ,
73 },
74};
75
76static struct smsc911x_platform_config smsc911x_config = {
77 .irq_polarity = SMSC911X_IRQ_POLARITY_ACTIVE_LOW,
78 .flags = SMSC911X_USE_32BIT | SMSC911X_FORCE_INTERNAL_PHY,
79};
80
81static struct platform_device smsc_lan9217_device = {
82 .name = "smsc911x",
Fabio Estevamf0df8752012-03-26 15:53:57 -030083 .id = -1,
Jason Wangfa94f8d2010-06-24 21:11:28 +080084 .dev = {
85 .platform_data = &smsc911x_config,
86 },
87 .num_resources = ARRAY_SIZE(smsc911x_resources),
88 .resource = smsc911x_resources,
89};
90
91static void mxc_expio_irq_handler(u32 irq, struct irq_desc *desc)
92{
93 u32 imr_val;
94 u32 int_valid;
95 u32 expio_irq;
96
Lennert Buytenhek4d935792010-11-29 11:16:23 +010097 /* irq = gpio irq number */
98 desc->irq_data.chip->irq_mask(&desc->irq_data);
Jason Wangfa94f8d2010-06-24 21:11:28 +080099
100 imr_val = __raw_readw(brd_io + INTR_MASK_REG);
101 int_valid = __raw_readw(brd_io + INTR_STATUS_REG) & ~imr_val;
102
103 expio_irq = MXC_BOARD_IRQ_START;
104 for (; int_valid != 0; int_valid >>= 1, expio_irq++) {
Jason Wangfa94f8d2010-06-24 21:11:28 +0800105 if ((int_valid & 1) == 0)
106 continue;
Thomas Gleixnereb2d7182011-03-24 12:43:25 +0100107 generic_handle_irq(expio_irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800108 }
109
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100110 desc->irq_data.chip->irq_ack(&desc->irq_data);
111 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800112}
113
114/*
115 * Disable an expio pin's interrupt by setting the bit in the imr.
116 * Irq is an expio virtual irq number
117 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100118static void expio_mask_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800119{
120 u16 reg;
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100121 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800122
123 reg = __raw_readw(brd_io + INTR_MASK_REG);
124 reg |= (1 << expio);
125 __raw_writew(reg, brd_io + INTR_MASK_REG);
126}
127
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100128static void expio_ack_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800129{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100130 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800131
132 __raw_writew(1 << expio, brd_io + INTR_RESET_REG);
133 __raw_writew(0, brd_io + INTR_RESET_REG);
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100134 expio_mask_irq(d);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800135}
136
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100137static void expio_unmask_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800138{
139 u16 reg;
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100140 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800141
142 reg = __raw_readw(brd_io + INTR_MASK_REG);
143 reg &= ~(1 << expio);
144 __raw_writew(reg, brd_io + INTR_MASK_REG);
145}
146
147static struct irq_chip expio_irq_chip = {
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100148 .irq_ack = expio_ack_irq,
149 .irq_mask = expio_mask_irq,
150 .irq_unmask = expio_unmask_irq,
Jason Wangfa94f8d2010-06-24 21:11:28 +0800151};
152
Sascha Hauer33499df2012-03-03 12:40:04 +0100153static struct regulator_consumer_supply dummy_supplies[] = {
154 REGULATOR_SUPPLY("vdd33a", "smsc911x"),
155 REGULATOR_SUPPLY("vddvario", "smsc911x"),
156};
157
Jason Wangfa94f8d2010-06-24 21:11:28 +0800158int __init mxc_expio_init(u32 base, u32 p_irq)
159{
160 int i;
161
162 brd_io = ioremap(BOARD_IO_ADDR(base), SZ_4K);
163 if (brd_io == NULL)
164 return -ENOMEM;
165
166 if ((__raw_readw(brd_io + MAGIC_NUMBER1_REG) != 0xAAAA) ||
167 (__raw_readw(brd_io + MAGIC_NUMBER2_REG) != 0x5555) ||
168 (__raw_readw(brd_io + MAGIC_NUMBER3_REG) != 0xCAFE)) {
169 pr_info("3-Stack Debug board not detected\n");
170 iounmap(brd_io);
171 brd_io = NULL;
172 return -ENODEV;
173 }
174
175 pr_info("3-Stack Debug board detected, rev = 0x%04X\n",
176 readw(brd_io + CPLD_CODE_VER_REG));
177
178 /*
179 * Configure INT line as GPIO input
180 */
181 gpio_request(MXC_IRQ_TO_GPIO(p_irq), "expio_pirq");
182 gpio_direction_input(MXC_IRQ_TO_GPIO(p_irq));
183
184 /* disable the interrupt and clear the status */
185 __raw_writew(0, brd_io + INTR_MASK_REG);
186 __raw_writew(0xFFFF, brd_io + INTR_RESET_REG);
187 __raw_writew(0, brd_io + INTR_RESET_REG);
188 __raw_writew(0x1F, brd_io + INTR_MASK_REG);
189 for (i = MXC_EXP_IO_BASE;
190 i < (MXC_EXP_IO_BASE + MXC_MAX_EXP_IO_LINES); i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100191 irq_set_chip_and_handler(i, &expio_irq_chip, handle_level_irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800192 set_irq_flags(i, IRQF_VALID);
193 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100194 irq_set_irq_type(p_irq, IRQF_TRIGGER_LOW);
195 irq_set_chained_handler(p_irq, mxc_expio_irq_handler);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800196
197 /* Register Lan device on the debugboard */
Sascha Hauer33499df2012-03-03 12:40:04 +0100198 regulator_register_fixed(0, dummy_supplies, ARRAY_SIZE(dummy_supplies));
199
Jason Wangfa94f8d2010-06-24 21:11:28 +0800200 smsc911x_resources[0].start = LAN9217_BASE_ADDR(base);
201 smsc911x_resources[0].end = LAN9217_BASE_ADDR(base) + 0x100 - 1;
202 platform_device_register(&smsc_lan9217_device);
203
204 return 0;
205}