blob: 3b48a08c165a73a7d629aeea82d549b6c61d6f60 [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)
Jason Wangfa94f8d2010-06-24 21:11:28 +080052
53#define MXC_EXP_IO_BASE (MXC_BOARD_IRQ_START)
54#define MXC_MAX_EXP_IO_LINES 16
55
56/* interrupts like external uart , external ethernet etc*/
57#define EXPIO_INT_ENET (MXC_BOARD_IRQ_START + 0)
58#define EXPIO_INT_XUART_A (MXC_BOARD_IRQ_START + 1)
59#define EXPIO_INT_XUART_B (MXC_BOARD_IRQ_START + 2)
60#define EXPIO_INT_BUTTON_A (MXC_BOARD_IRQ_START + 3)
61#define EXPIO_INT_BUTTON_B (MXC_BOARD_IRQ_START + 4)
62
63static void __iomem *brd_io;
Jason Wangfa94f8d2010-06-24 21:11:28 +080064
65static struct resource smsc911x_resources[] = {
66 {
67 .flags = IORESOURCE_MEM,
68 } , {
69 .start = EXPIO_INT_ENET,
70 .end = EXPIO_INT_ENET,
71 .flags = IORESOURCE_IRQ,
72 },
73};
74
75static struct smsc911x_platform_config smsc911x_config = {
76 .irq_polarity = SMSC911X_IRQ_POLARITY_ACTIVE_LOW,
77 .flags = SMSC911X_USE_32BIT | SMSC911X_FORCE_INTERNAL_PHY,
78};
79
80static struct platform_device smsc_lan9217_device = {
81 .name = "smsc911x",
Fabio Estevamf0df8752012-03-26 15:53:57 -030082 .id = -1,
Jason Wangfa94f8d2010-06-24 21:11:28 +080083 .dev = {
84 .platform_data = &smsc911x_config,
85 },
86 .num_resources = ARRAY_SIZE(smsc911x_resources),
87 .resource = smsc911x_resources,
88};
89
90static void mxc_expio_irq_handler(u32 irq, struct irq_desc *desc)
91{
92 u32 imr_val;
93 u32 int_valid;
94 u32 expio_irq;
95
Lennert Buytenhek4d935792010-11-29 11:16:23 +010096 /* irq = gpio irq number */
97 desc->irq_data.chip->irq_mask(&desc->irq_data);
Jason Wangfa94f8d2010-06-24 21:11:28 +080098
99 imr_val = __raw_readw(brd_io + INTR_MASK_REG);
100 int_valid = __raw_readw(brd_io + INTR_STATUS_REG) & ~imr_val;
101
102 expio_irq = MXC_BOARD_IRQ_START;
103 for (; int_valid != 0; int_valid >>= 1, expio_irq++) {
Jason Wangfa94f8d2010-06-24 21:11:28 +0800104 if ((int_valid & 1) == 0)
105 continue;
Thomas Gleixnereb2d7182011-03-24 12:43:25 +0100106 generic_handle_irq(expio_irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800107 }
108
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100109 desc->irq_data.chip->irq_ack(&desc->irq_data);
110 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800111}
112
113/*
114 * Disable an expio pin's interrupt by setting the bit in the imr.
115 * Irq is an expio virtual irq number
116 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100117static void expio_mask_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800118{
119 u16 reg;
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100120 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800121
122 reg = __raw_readw(brd_io + INTR_MASK_REG);
123 reg |= (1 << expio);
124 __raw_writew(reg, brd_io + INTR_MASK_REG);
125}
126
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100127static void expio_ack_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800128{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100129 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800130
131 __raw_writew(1 << expio, brd_io + INTR_RESET_REG);
132 __raw_writew(0, brd_io + INTR_RESET_REG);
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100133 expio_mask_irq(d);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800134}
135
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100136static void expio_unmask_irq(struct irq_data *d)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800137{
138 u16 reg;
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100139 u32 expio = MXC_IRQ_TO_EXPIO(d->irq);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800140
141 reg = __raw_readw(brd_io + INTR_MASK_REG);
142 reg &= ~(1 << expio);
143 __raw_writew(reg, brd_io + INTR_MASK_REG);
144}
145
146static struct irq_chip expio_irq_chip = {
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100147 .irq_ack = expio_ack_irq,
148 .irq_mask = expio_mask_irq,
149 .irq_unmask = expio_unmask_irq,
Jason Wangfa94f8d2010-06-24 21:11:28 +0800150};
151
Sascha Hauer33499df2012-03-03 12:40:04 +0100152static struct regulator_consumer_supply dummy_supplies[] = {
153 REGULATOR_SUPPLY("vdd33a", "smsc911x"),
154 REGULATOR_SUPPLY("vddvario", "smsc911x"),
155};
156
Shawn Guoed4a7fb2012-06-13 15:58:08 +0800157int __init mxc_expio_init(u32 base, u32 intr_gpio)
Jason Wangfa94f8d2010-06-24 21:11:28 +0800158{
Shawn Guoed4a7fb2012-06-13 15:58:08 +0800159 u32 p_irq = gpio_to_irq(intr_gpio);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800160 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 */
Shawn Guoed4a7fb2012-06-13 15:58:08 +0800181 gpio_request(intr_gpio, "expio_pirq");
182 gpio_direction_input(intr_gpio);
Jason Wangfa94f8d2010-06-24 21:11:28 +0800183
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}