Commit | Line | Data |
---|---|---|
fa94f8dc JW |
1 | /* |
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> | |
33499df8 SH |
19 | #include <linux/regulator/machine.h> |
20 | #include <linux/regulator/fixed.h> | |
fa94f8dc JW |
21 | |
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 | ||
64 | static void __iomem *brd_io; | |
fa94f8dc JW |
65 | |
66 | static 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 | ||
76 | static 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 | ||
81 | static struct platform_device smsc_lan9217_device = { | |
82 | .name = "smsc911x", | |
f0df8754 | 83 | .id = -1, |
fa94f8dc JW |
84 | .dev = { |
85 | .platform_data = &smsc911x_config, | |
86 | }, | |
87 | .num_resources = ARRAY_SIZE(smsc911x_resources), | |
88 | .resource = smsc911x_resources, | |
89 | }; | |
90 | ||
91 | static 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 | ||
4d93579f LB |
97 | /* irq = gpio irq number */ |
98 | desc->irq_data.chip->irq_mask(&desc->irq_data); | |
fa94f8dc JW |
99 | |
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++) { | |
fa94f8dc JW |
105 | if ((int_valid & 1) == 0) |
106 | continue; | |
eb2d7188 | 107 | generic_handle_irq(expio_irq); |
fa94f8dc JW |
108 | } |
109 | ||
4d93579f LB |
110 | desc->irq_data.chip->irq_ack(&desc->irq_data); |
111 | desc->irq_data.chip->irq_unmask(&desc->irq_data); | |
fa94f8dc JW |
112 | } |
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 | */ | |
4d93579f | 118 | static void expio_mask_irq(struct irq_data *d) |
fa94f8dc JW |
119 | { |
120 | u16 reg; | |
4d93579f | 121 | u32 expio = MXC_IRQ_TO_EXPIO(d->irq); |
fa94f8dc JW |
122 | |
123 | reg = __raw_readw(brd_io + INTR_MASK_REG); | |
124 | reg |= (1 << expio); | |
125 | __raw_writew(reg, brd_io + INTR_MASK_REG); | |
126 | } | |
127 | ||
4d93579f | 128 | static void expio_ack_irq(struct irq_data *d) |
fa94f8dc | 129 | { |
4d93579f | 130 | u32 expio = MXC_IRQ_TO_EXPIO(d->irq); |
fa94f8dc JW |
131 | |
132 | __raw_writew(1 << expio, brd_io + INTR_RESET_REG); | |
133 | __raw_writew(0, brd_io + INTR_RESET_REG); | |
4d93579f | 134 | expio_mask_irq(d); |
fa94f8dc JW |
135 | } |
136 | ||
4d93579f | 137 | static void expio_unmask_irq(struct irq_data *d) |
fa94f8dc JW |
138 | { |
139 | u16 reg; | |
4d93579f | 140 | u32 expio = MXC_IRQ_TO_EXPIO(d->irq); |
fa94f8dc JW |
141 | |
142 | reg = __raw_readw(brd_io + INTR_MASK_REG); | |
143 | reg &= ~(1 << expio); | |
144 | __raw_writew(reg, brd_io + INTR_MASK_REG); | |
145 | } | |
146 | ||
147 | static struct irq_chip expio_irq_chip = { | |
4d93579f LB |
148 | .irq_ack = expio_ack_irq, |
149 | .irq_mask = expio_mask_irq, | |
150 | .irq_unmask = expio_unmask_irq, | |
fa94f8dc JW |
151 | }; |
152 | ||
33499df8 SH |
153 | static struct regulator_consumer_supply dummy_supplies[] = { |
154 | REGULATOR_SUPPLY("vdd33a", "smsc911x"), | |
155 | REGULATOR_SUPPLY("vddvario", "smsc911x"), | |
156 | }; | |
157 | ||
fa94f8dc JW |
158 | int __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++) { | |
f38c02f3 | 191 | irq_set_chip_and_handler(i, &expio_irq_chip, handle_level_irq); |
fa94f8dc JW |
192 | set_irq_flags(i, IRQF_VALID); |
193 | } | |
6845664a TG |
194 | irq_set_irq_type(p_irq, IRQF_TRIGGER_LOW); |
195 | irq_set_chained_handler(p_irq, mxc_expio_irq_handler); | |
fa94f8dc JW |
196 | |
197 | /* Register Lan device on the debugboard */ | |
33499df8 SH |
198 | regulator_register_fixed(0, dummy_supplies, ARRAY_SIZE(dummy_supplies)); |
199 | ||
fa94f8dc JW |
200 | 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 | } |