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