setup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Board-specific setup code for the Merisc
  3. *
  4. * Copyright (C) 2008 Martinsson Elektronik AB
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/i2c.h>
  13. #include <linux/i2c-gpio.h>
  14. #include <linux/gpio.h>
  15. #include <linux/init.h>
  16. #include <linux/linkage.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #include <linux/leds.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/ads7846.h>
  22. #include <linux/irq.h>
  23. #include <linux/fb.h>
  24. #include <linux/atmel-mci.h>
  25. #include <linux/pwm.h>
  26. #include <linux/leds_pwm.h>
  27. #include <asm/io.h>
  28. #include <asm/setup.h>
  29. #include <mach/at32ap700x.h>
  30. #include <mach/board.h>
  31. #include <mach/init.h>
  32. #include <mach/portmux.h>
  33. #include "merisc.h"
  34. /* Holds the autodetected board model and revision */
  35. static int merisc_board_id;
  36. /* Initialized by bootloader-specific startup code. */
  37. struct tag *bootloader_tags __initdata;
  38. /* Oscillator frequencies. These are board specific */
  39. unsigned long at32_board_osc_rates[3] = {
  40. [0] = 32768, /* 32.768 kHz on RTC osc */
  41. [1] = 20000000, /* 20 MHz on osc0 */
  42. [2] = 12000000, /* 12 MHz on osc1 */
  43. };
  44. struct eth_addr {
  45. u8 addr[6];
  46. };
  47. static struct eth_addr __initdata hw_addr[2];
  48. static struct macb_platform_data __initdata eth_data[2];
  49. static int ads7846_get_pendown_state_PB26(void)
  50. {
  51. return !gpio_get_value(GPIO_PIN_PB(26));
  52. }
  53. static int ads7846_get_pendown_state_PB28(void)
  54. {
  55. return !gpio_get_value(GPIO_PIN_PB(28));
  56. }
  57. static struct ads7846_platform_data __initdata ads7846_data = {
  58. .model = 7846,
  59. .vref_delay_usecs = 100,
  60. .vref_mv = 0,
  61. .keep_vref_on = 0,
  62. .settle_delay_usecs = 150,
  63. .penirq_recheck_delay_usecs = 1,
  64. .x_plate_ohms = 800,
  65. .debounce_rep = 4,
  66. .debounce_max = 10,
  67. .debounce_tol = 50,
  68. .get_pendown_state = ads7846_get_pendown_state_PB26,
  69. .filter_init = NULL,
  70. .filter = NULL,
  71. .filter_cleanup = NULL,
  72. };
  73. static struct spi_board_info __initdata spi0_board_info[] = {
  74. {
  75. .modalias = "ads7846",
  76. .max_speed_hz = 3250000,
  77. .chip_select = 0,
  78. .bus_num = 0,
  79. .platform_data = &ads7846_data,
  80. .mode = SPI_MODE_0,
  81. },
  82. };
  83. static struct mci_platform_data __initdata mci0_data = {
  84. .slot[0] = {
  85. .bus_width = 4,
  86. .detect_pin = GPIO_PIN_PE(19),
  87. .wp_pin = GPIO_PIN_PE(20),
  88. .detect_is_active_high = true,
  89. },
  90. };
  91. static int __init parse_tag_ethernet(struct tag *tag)
  92. {
  93. int i;
  94. i = tag->u.ethernet.mac_index;
  95. if (i < ARRAY_SIZE(hw_addr)) {
  96. memcpy(hw_addr[i].addr, tag->u.ethernet.hw_address,
  97. sizeof(hw_addr[i].addr));
  98. }
  99. return 0;
  100. }
  101. __tagtable(ATAG_ETHERNET, parse_tag_ethernet);
  102. static void __init set_hw_addr(struct platform_device *pdev)
  103. {
  104. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  105. const u8 *addr;
  106. void __iomem *regs;
  107. struct clk *pclk;
  108. if (!res)
  109. return;
  110. if (pdev->id >= ARRAY_SIZE(hw_addr))
  111. return;
  112. addr = hw_addr[pdev->id].addr;
  113. if (!is_valid_ether_addr(addr))
  114. return;
  115. regs = (void __iomem __force *)res->start;
  116. pclk = clk_get(&pdev->dev, "pclk");
  117. if (IS_ERR(pclk))
  118. return;
  119. clk_enable(pclk);
  120. __raw_writel((addr[3] << 24) | (addr[2] << 16)
  121. | (addr[1] << 8) | addr[0], regs + 0x98);
  122. __raw_writel((addr[5] << 8) | addr[4], regs + 0x9c);
  123. clk_disable(pclk);
  124. clk_put(pclk);
  125. }
  126. static struct i2c_gpio_platform_data i2c_gpio_data = {
  127. .sda_pin = GPIO_PIN_PA(6),
  128. .scl_pin = GPIO_PIN_PA(7),
  129. .sda_is_open_drain = 1,
  130. .scl_is_open_drain = 1,
  131. .udelay = 2,
  132. };
  133. static struct platform_device i2c_gpio_device = {
  134. .name = "i2c-gpio",
  135. .id = 0,
  136. .dev = {
  137. .platform_data = &i2c_gpio_data,
  138. },
  139. };
  140. static struct i2c_board_info __initdata i2c_info[] = {
  141. {
  142. I2C_BOARD_INFO("pcf8563", 0x51)
  143. },
  144. };
  145. #if IS_ENABLED(CONFIG_LEDS_PWM)
  146. static struct pwm_lookup pwm_lookup[] = {
  147. PWM_LOOKUP("at91sam9rl-pwm", 0, "leds_pwm", "backlight",
  148. 5000, PWM_POLARITY_NORMAL),
  149. };
  150. static struct led_pwm pwm_leds[] = {
  151. {
  152. .name = "backlight",
  153. .max_brightness = 255,
  154. },
  155. };
  156. static struct led_pwm_platform_data pwm_data = {
  157. .num_leds = ARRAY_SIZE(pwm_leds),
  158. .leds = pwm_leds,
  159. };
  160. static struct platform_device leds_pwm = {
  161. .name = "leds_pwm",
  162. .id = -1,
  163. .dev = {
  164. .platform_data = &pwm_data,
  165. },
  166. };
  167. #endif
  168. const char *merisc_model(void)
  169. {
  170. switch (merisc_board_id) {
  171. case 0:
  172. case 1:
  173. return "500-01";
  174. case 2:
  175. return "BT";
  176. default:
  177. return "Unknown";
  178. }
  179. }
  180. const char *merisc_revision(void)
  181. {
  182. switch (merisc_board_id) {
  183. case 0:
  184. return "B";
  185. case 1:
  186. return "D";
  187. case 2:
  188. return "A";
  189. default:
  190. return "Unknown";
  191. }
  192. }
  193. static void detect_merisc_board_id(void)
  194. {
  195. /* Board ID pins MUST be set as input or the board may be damaged */
  196. at32_select_gpio(GPIO_PIN_PA(24), AT32_GPIOF_PULLUP);
  197. at32_select_gpio(GPIO_PIN_PA(25), AT32_GPIOF_PULLUP);
  198. at32_select_gpio(GPIO_PIN_PA(26), AT32_GPIOF_PULLUP);
  199. at32_select_gpio(GPIO_PIN_PA(27), AT32_GPIOF_PULLUP);
  200. merisc_board_id = !gpio_get_value(GPIO_PIN_PA(24)) +
  201. !gpio_get_value(GPIO_PIN_PA(25)) * 2 +
  202. !gpio_get_value(GPIO_PIN_PA(26)) * 4 +
  203. !gpio_get_value(GPIO_PIN_PA(27)) * 8;
  204. }
  205. void __init setup_board(void)
  206. {
  207. at32_map_usart(0, 0, 0);
  208. at32_map_usart(1, 1, 0);
  209. at32_map_usart(3, 3, 0);
  210. at32_setup_serial_console(1);
  211. }
  212. static int __init merisc_init(void)
  213. {
  214. detect_merisc_board_id();
  215. printk(KERN_NOTICE "BOARD: Merisc %s revision %s\n", merisc_model(),
  216. merisc_revision());
  217. /* Reserve pins for SDRAM */
  218. at32_reserve_pin(GPIO_PIOE_BASE, ATMEL_EBI_PE_DATA_ALL | (1 << 26));
  219. if (merisc_board_id >= 1)
  220. at32_map_usart(2, 2, 0);
  221. at32_add_device_usart(0);
  222. at32_add_device_usart(1);
  223. if (merisc_board_id >= 1)
  224. at32_add_device_usart(2);
  225. at32_add_device_usart(3);
  226. set_hw_addr(at32_add_device_eth(0, &eth_data[0]));
  227. /* ADS7846 PENIRQ */
  228. if (merisc_board_id == 0) {
  229. ads7846_data.get_pendown_state = ads7846_get_pendown_state_PB26;
  230. at32_select_periph(GPIO_PIOB_BASE, 1 << 26,
  231. GPIO_PERIPH_A, AT32_GPIOF_PULLUP);
  232. spi0_board_info[0].irq = AT32_EXTINT(1);
  233. } else {
  234. ads7846_data.get_pendown_state = ads7846_get_pendown_state_PB28;
  235. at32_select_periph(GPIO_PIOB_BASE, 1 << 28, GPIO_PERIPH_A,
  236. AT32_GPIOF_PULLUP);
  237. spi0_board_info[0].irq = AT32_EXTINT(3);
  238. }
  239. /* ADS7846 busy pin */
  240. at32_select_gpio(GPIO_PIN_PA(4), AT32_GPIOF_PULLUP);
  241. at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info));
  242. at32_add_device_mci(0, &mci0_data);
  243. #if IS_ENABLED(CONFIG_LEDS_PWM)
  244. pwm_add_table(pwm_lookup, ARRAY_SIZE(pwm_lookup));
  245. at32_add_device_pwm((1 << 0) | (1 << 2));
  246. platform_device_register(&leds_pwm);
  247. #else
  248. at32_add_device_pwm((1 << 2));
  249. #endif
  250. at32_select_gpio(i2c_gpio_data.sda_pin,
  251. AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  252. at32_select_gpio(i2c_gpio_data.scl_pin,
  253. AT32_GPIOF_MULTIDRV | AT32_GPIOF_OUTPUT | AT32_GPIOF_HIGH);
  254. platform_device_register(&i2c_gpio_device);
  255. i2c_register_board_info(0, i2c_info, ARRAY_SIZE(i2c_info));
  256. return 0;
  257. }
  258. postcore_initcall(merisc_init);