Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/mach-imx/generic.c |
| 3 | * |
| 4 | * author: Sascha Hauer |
| 5 | * Created: april 20th, 2004 |
| 6 | * Copyright: Synertronixx GmbH |
| 7 | * |
| 8 | * Common code for i.MX machines |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 29 | #include <linux/string.h> |
| 30 | |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 31 | #include <asm/errno.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 32 | #include <mach/hardware.h> |
| 33 | #include <mach/imx-regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <asm/mach/map.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 36 | #include <mach/mmc.h> |
| 37 | #include <mach/gpio.h> |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 38 | |
| 39 | unsigned long imx_gpio_alloc_map[(GPIO_PORT_MAX + 1) * 32 / BITS_PER_LONG]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | void imx_gpio_mode(int gpio_mode) |
| 42 | { |
| 43 | unsigned int pin = gpio_mode & GPIO_PIN_MASK; |
Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 44 | unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT; |
| 45 | unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | unsigned int tmp; |
| 47 | |
| 48 | /* Pullup enable */ |
| 49 | if(gpio_mode & GPIO_PUEN) |
| 50 | PUEN(port) |= (1<<pin); |
| 51 | else |
| 52 | PUEN(port) &= ~(1<<pin); |
| 53 | |
| 54 | /* Data direction */ |
| 55 | if(gpio_mode & GPIO_OUT) |
| 56 | DDIR(port) |= 1<<pin; |
| 57 | else |
| 58 | DDIR(port) &= ~(1<<pin); |
| 59 | |
| 60 | /* Primary / alternate function */ |
| 61 | if(gpio_mode & GPIO_AF) |
| 62 | GPR(port) |= (1<<pin); |
| 63 | else |
| 64 | GPR(port) &= ~(1<<pin); |
| 65 | |
| 66 | /* use as gpio? */ |
Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 67 | if(gpio_mode & GPIO_GIUS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | GIUS(port) |= (1<<pin); |
| 69 | else |
| 70 | GIUS(port) &= ~(1<<pin); |
| 71 | |
| 72 | /* Output / input configuration */ |
| 73 | /* FIXME: I'm not very sure about OCR and ICONF, someone |
| 74 | * should have a look over it |
| 75 | */ |
| 76 | if(pin<16) { |
| 77 | tmp = OCR1(port); |
| 78 | tmp &= ~( 3<<(pin*2)); |
| 79 | tmp |= (ocr << (pin*2)); |
| 80 | OCR1(port) = tmp; |
| 81 | |
Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 82 | ICONFA1(port) &= ~( 3<<(pin*2)); |
| 83 | ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2); |
| 84 | ICONFB1(port) &= ~( 3<<(pin*2)); |
| 85 | ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } else { |
| 87 | tmp = OCR2(port); |
| 88 | tmp &= ~( 3<<((pin-16)*2)); |
| 89 | tmp |= (ocr << ((pin-16)*2)); |
| 90 | OCR2(port) = tmp; |
| 91 | |
Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 92 | ICONFA2(port) &= ~( 3<<((pin-16)*2)); |
| 93 | ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2); |
| 94 | ICONFB2(port) &= ~( 3<<((pin-16)*2)); |
| 95 | ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | EXPORT_SYMBOL(imx_gpio_mode); |
| 100 | |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 101 | int imx_gpio_request(unsigned gpio, const char *label) |
| 102 | { |
Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 103 | if(gpio >= (GPIO_PORT_MAX + 1) * 32) { |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 104 | printk(KERN_ERR "imx_gpio: Attempt to request nonexistent GPIO %d for \"%s\"\n", |
| 105 | gpio, label ? label : "?"); |
| 106 | return -EINVAL; |
Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 107 | } |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 108 | |
| 109 | if(test_and_set_bit(gpio, imx_gpio_alloc_map)) { |
| 110 | printk(KERN_ERR "imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n", |
| 111 | gpio, label ? label : "?"); |
| 112 | return -EBUSY; |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | EXPORT_SYMBOL(imx_gpio_request); |
| 119 | |
| 120 | void imx_gpio_free(unsigned gpio) |
| 121 | { |
| 122 | if(gpio >= (GPIO_PORT_MAX + 1) * 32) |
| 123 | return; |
| 124 | |
| 125 | clear_bit(gpio, imx_gpio_alloc_map); |
| 126 | } |
| 127 | |
| 128 | EXPORT_SYMBOL(imx_gpio_free); |
| 129 | |
| 130 | int imx_gpio_direction_input(unsigned gpio) |
| 131 | { |
Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 132 | imx_gpio_mode(gpio | GPIO_IN | GPIO_GIUS | GPIO_DR); |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL(imx_gpio_direction_input); |
| 137 | |
| 138 | int imx_gpio_direction_output(unsigned gpio, int value) |
| 139 | { |
| 140 | imx_gpio_set_value(gpio, value); |
Pavel Pisa | a228d6e | 2007-08-20 21:39:41 +0100 | [diff] [blame] | 141 | imx_gpio_mode(gpio | GPIO_OUT | GPIO_GIUS | GPIO_DR); |
Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | EXPORT_SYMBOL(imx_gpio_direction_output); |
| 146 | |
| 147 | int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count, |
| 148 | int alloc_mode, const char *label) |
| 149 | { |
| 150 | const int *p = pin_list; |
| 151 | int i; |
| 152 | unsigned gpio; |
| 153 | unsigned mode; |
| 154 | |
| 155 | for (i = 0; i < count; i++) { |
| 156 | gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK); |
| 157 | mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK); |
| 158 | |
| 159 | if (gpio >= (GPIO_PORT_MAX + 1) * 32) |
| 160 | goto setup_error; |
| 161 | |
| 162 | if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE) |
| 163 | imx_gpio_free(gpio); |
| 164 | else if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_NO_ALLOC)) |
| 165 | if (imx_gpio_request(gpio, label)) |
| 166 | if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_TRY_ALLOC)) |
| 167 | goto setup_error; |
| 168 | |
| 169 | if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY | |
| 170 | IMX_GPIO_ALLOC_MODE_RELEASE))) |
| 171 | imx_gpio_mode(gpio | mode); |
| 172 | |
| 173 | p++; |
| 174 | } |
| 175 | return 0; |
| 176 | |
| 177 | setup_error: |
| 178 | if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC | |
| 179 | IMX_GPIO_ALLOC_MODE_TRY_ALLOC)) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | while (p != pin_list) { |
| 183 | p--; |
| 184 | gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK); |
| 185 | imx_gpio_free(gpio); |
| 186 | } |
| 187 | |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | |
| 191 | EXPORT_SYMBOL(imx_gpio_setup_multiple_pins); |
| 192 | |
| 193 | void __imx_gpio_set_value(unsigned gpio, int value) |
| 194 | { |
| 195 | imx_gpio_set_value_inline(gpio, value); |
| 196 | } |
| 197 | |
| 198 | EXPORT_SYMBOL(__imx_gpio_set_value); |
| 199 | |
| 200 | int imx_gpio_to_irq(unsigned gpio) |
| 201 | { |
| 202 | return IRQ_GPIOA(0) + gpio; |
| 203 | } |
| 204 | |
| 205 | EXPORT_SYMBOL(imx_gpio_to_irq); |
| 206 | |
| 207 | int imx_irq_to_gpio(unsigned irq) |
| 208 | { |
| 209 | if (irq < IRQ_GPIOA(0)) |
| 210 | return -EINVAL; |
| 211 | return irq - IRQ_GPIOA(0); |
| 212 | } |
| 213 | |
| 214 | EXPORT_SYMBOL(imx_irq_to_gpio); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | static struct resource imx_mmc_resources[] = { |
| 217 | [0] = { |
| 218 | .start = 0x00214000, |
| 219 | .end = 0x002140FF, |
| 220 | .flags = IORESOURCE_MEM, |
| 221 | }, |
| 222 | [1] = { |
| 223 | .start = (SDHC_INT), |
| 224 | .end = (SDHC_INT), |
| 225 | .flags = IORESOURCE_IRQ, |
| 226 | }, |
| 227 | }; |
| 228 | |
Pavel Pisa | 56ca9040 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 229 | static u64 imxmmmc_dmamask = 0xffffffffUL; |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | static struct platform_device imx_mmc_device = { |
| 232 | .name = "imx-mmc", |
| 233 | .id = 0, |
Pavel Pisa | 56ca9040 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 234 | .dev = { |
| 235 | .dma_mask = &imxmmmc_dmamask, |
| 236 | .coherent_dma_mask = 0xffffffff, |
| 237 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | .num_resources = ARRAY_SIZE(imx_mmc_resources), |
| 239 | .resource = imx_mmc_resources, |
| 240 | }; |
| 241 | |
Pavel Pisa | 56ca9040 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 242 | void __init imx_set_mmc_info(struct imxmmc_platform_data *info) |
| 243 | { |
| 244 | imx_mmc_device.dev.platform_data = info; |
| 245 | } |
Pavel Pisa | 56ca9040 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | static struct platform_device *devices[] __initdata = { |
| 248 | &imx_mmc_device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | static struct map_desc imx_io_desc[] __initdata = { |
Deepak Saxena | adecef7 | 2005-10-28 15:19:10 +0100 | [diff] [blame] | 252 | { |
| 253 | .virtual = IMX_IO_BASE, |
| 254 | .pfn = __phys_to_pfn(IMX_IO_PHYS), |
| 255 | .length = IMX_IO_SIZE, |
| 256 | .type = MT_DEVICE |
| 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | void __init |
| 261 | imx_map_io(void) |
| 262 | { |
| 263 | iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc)); |
| 264 | } |
| 265 | |
| 266 | static int __init imx_init(void) |
| 267 | { |
| 268 | return platform_add_devices(devices, ARRAY_SIZE(devices)); |
| 269 | } |
| 270 | |
| 271 | subsys_initcall(imx_init); |