at91_gpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (C) 2013 Bo Shen <voice.shen@atmel.com>
  3. *
  4. * Copyright (C) 2009 Jens Scharsig (js_at_ng@scharsoft.de)
  5. *
  6. * Copyright (C) 2005 HP Labs
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <config.h>
  11. #include <common.h>
  12. #include <asm/io.h>
  13. #include <linux/sizes.h>
  14. #include <asm/arch/hardware.h>
  15. #include <asm/arch/at91_pio.h>
  16. #include <asm/arch/gpio.h>
  17. static struct at91_port *at91_pio_get_port(unsigned port)
  18. {
  19. switch (port) {
  20. case AT91_PIO_PORTA:
  21. return (struct at91_port *)ATMEL_BASE_PIOA;
  22. case AT91_PIO_PORTB:
  23. return (struct at91_port *)ATMEL_BASE_PIOB;
  24. case AT91_PIO_PORTC:
  25. return (struct at91_port *)ATMEL_BASE_PIOC;
  26. #if (ATMEL_PIO_PORTS > 3)
  27. case AT91_PIO_PORTD:
  28. return (struct at91_port *)ATMEL_BASE_PIOD;
  29. #if (ATMEL_PIO_PORTS > 4)
  30. case AT91_PIO_PORTE:
  31. return (struct at91_port *)ATMEL_BASE_PIOE;
  32. #endif
  33. #endif
  34. default:
  35. printf("Error: at91_gpio: Fail to get PIO base!\n");
  36. return NULL;
  37. }
  38. }
  39. int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
  40. {
  41. struct at91_port *at91_port = at91_pio_get_port(port);
  42. u32 mask;
  43. if (at91_port && (pin < 32)) {
  44. mask = 1 << pin;
  45. if (use_pullup)
  46. writel(1 << pin, &at91_port->puer);
  47. else
  48. writel(1 << pin, &at91_port->pudr);
  49. writel(mask, &at91_port->per);
  50. }
  51. return 0;
  52. }
  53. /*
  54. * mux the pin to the "GPIO" peripheral role.
  55. */
  56. int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
  57. {
  58. struct at91_port *at91_port = at91_pio_get_port(port);
  59. u32 mask;
  60. if (at91_port && (pin < 32)) {
  61. mask = 1 << pin;
  62. writel(mask, &at91_port->idr);
  63. at91_set_pio_pullup(port, pin, use_pullup);
  64. writel(mask, &at91_port->per);
  65. }
  66. return 0;
  67. }
  68. /*
  69. * mux the pin to the "A" internal peripheral role.
  70. */
  71. int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
  72. {
  73. struct at91_port *at91_port = at91_pio_get_port(port);
  74. u32 mask;
  75. if (at91_port && (pin < 32)) {
  76. mask = 1 << pin;
  77. writel(mask, &at91_port->idr);
  78. at91_set_pio_pullup(port, pin, use_pullup);
  79. #if defined(CPU_HAS_PIO3)
  80. writel(readl(&at91_port->abcdsr1) & ~mask,
  81. &at91_port->abcdsr1);
  82. writel(readl(&at91_port->abcdsr2) & ~mask,
  83. &at91_port->abcdsr2);
  84. #else
  85. writel(mask, &at91_port->asr);
  86. #endif
  87. writel(mask, &at91_port->pdr);
  88. }
  89. return 0;
  90. }
  91. /*
  92. * mux the pin to the "B" internal peripheral role.
  93. */
  94. int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
  95. {
  96. struct at91_port *at91_port = at91_pio_get_port(port);
  97. u32 mask;
  98. if (at91_port && (pin < 32)) {
  99. mask = 1 << pin;
  100. writel(mask, &at91_port->idr);
  101. at91_set_pio_pullup(port, pin, use_pullup);
  102. #if defined(CPU_HAS_PIO3)
  103. writel(readl(&at91_port->abcdsr1) | mask,
  104. &at91_port->abcdsr1);
  105. writel(readl(&at91_port->abcdsr2) & ~mask,
  106. &at91_port->abcdsr2);
  107. #else
  108. writel(mask, &at91_port->bsr);
  109. #endif
  110. writel(mask, &at91_port->pdr);
  111. }
  112. return 0;
  113. }
  114. #if defined(CPU_HAS_PIO3)
  115. /*
  116. * mux the pin to the "C" internal peripheral role.
  117. */
  118. int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
  119. {
  120. struct at91_port *at91_port = at91_pio_get_port(port);
  121. u32 mask;
  122. if (at91_port && (pin < 32)) {
  123. mask = 1 << pin;
  124. writel(mask, &at91_port->idr);
  125. at91_set_pio_pullup(port, pin, use_pullup);
  126. writel(readl(&at91_port->abcdsr1) & ~mask,
  127. &at91_port->abcdsr1);
  128. writel(readl(&at91_port->abcdsr2) | mask,
  129. &at91_port->abcdsr2);
  130. writel(mask, &at91_port->pdr);
  131. }
  132. return 0;
  133. }
  134. /*
  135. * mux the pin to the "D" internal peripheral role.
  136. */
  137. int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
  138. {
  139. struct at91_port *at91_port = at91_pio_get_port(port);
  140. u32 mask;
  141. if (at91_port && (pin < 32)) {
  142. mask = 1 << pin;
  143. writel(mask, &at91_port->idr);
  144. at91_set_pio_pullup(port, pin, use_pullup);
  145. writel(readl(&at91_port->abcdsr1) | mask,
  146. &at91_port->abcdsr1);
  147. writel(readl(&at91_port->abcdsr2) | mask,
  148. &at91_port->abcdsr2);
  149. writel(mask, &at91_port->pdr);
  150. }
  151. return 0;
  152. }
  153. #endif
  154. /*
  155. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  156. * configure it for an input.
  157. */
  158. int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
  159. {
  160. struct at91_port *at91_port = at91_pio_get_port(port);
  161. u32 mask;
  162. if (at91_port && (pin < 32)) {
  163. mask = 1 << pin;
  164. writel(mask, &at91_port->idr);
  165. at91_set_pio_pullup(port, pin, use_pullup);
  166. writel(mask, &at91_port->odr);
  167. writel(mask, &at91_port->per);
  168. }
  169. return 0;
  170. }
  171. /*
  172. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  173. * and configure it for an output.
  174. */
  175. int at91_set_pio_output(unsigned port, u32 pin, int value)
  176. {
  177. struct at91_port *at91_port = at91_pio_get_port(port);
  178. u32 mask;
  179. if (at91_port && (port < ATMEL_PIO_PORTS) && (pin < 32)) {
  180. mask = 1 << pin;
  181. writel(mask, &at91_port->idr);
  182. writel(mask, &at91_port->pudr);
  183. if (value)
  184. writel(mask, &at91_port->sodr);
  185. else
  186. writel(mask, &at91_port->codr);
  187. writel(mask, &at91_port->oer);
  188. writel(mask, &at91_port->per);
  189. }
  190. return 0;
  191. }
  192. /*
  193. * enable/disable the glitch filter. mostly used with IRQ handling.
  194. */
  195. int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
  196. {
  197. struct at91_port *at91_port = at91_pio_get_port(port);
  198. u32 mask;
  199. if (at91_port && (pin < 32)) {
  200. mask = 1 << pin;
  201. if (is_on) {
  202. #if defined(CPU_HAS_PIO3)
  203. writel(mask, &at91_port->ifscdr);
  204. #endif
  205. writel(mask, &at91_port->ifer);
  206. } else {
  207. writel(mask, &at91_port->ifdr);
  208. }
  209. }
  210. return 0;
  211. }
  212. #if defined(CPU_HAS_PIO3)
  213. /*
  214. * enable/disable the debounce filter.
  215. */
  216. int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
  217. {
  218. struct at91_port *at91_port = at91_pio_get_port(port);
  219. u32 mask;
  220. if (at91_port && (pin < 32)) {
  221. mask = 1 << pin;
  222. if (is_on) {
  223. writel(mask, &at91_port->ifscer);
  224. writel(div & PIO_SCDR_DIV, &at91_port->scdr);
  225. writel(mask, &at91_port->ifer);
  226. } else {
  227. writel(mask, &at91_port->ifdr);
  228. }
  229. }
  230. return 0;
  231. }
  232. /*
  233. * enable/disable the pull-down.
  234. * If pull-up already enabled while calling the function, we disable it.
  235. */
  236. int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
  237. {
  238. struct at91_port *at91_port = at91_pio_get_port(port);
  239. u32 mask;
  240. if (at91_port && (pin < 32)) {
  241. mask = 1 << pin;
  242. writel(mask, &at91_port->pudr);
  243. if (is_on)
  244. writel(mask, &at91_port->ppder);
  245. else
  246. writel(mask, &at91_port->ppddr);
  247. }
  248. return 0;
  249. }
  250. /*
  251. * disable Schmitt trigger
  252. */
  253. int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
  254. {
  255. struct at91_port *at91_port = at91_pio_get_port(port);
  256. u32 mask;
  257. if (at91_port && (pin < 32)) {
  258. mask = 1 << pin;
  259. writel(readl(&at91_port->schmitt) | mask,
  260. &at91_port->schmitt);
  261. }
  262. return 0;
  263. }
  264. #endif
  265. /*
  266. * enable/disable the multi-driver. This is only valid for output and
  267. * allows the output pin to run as an open collector output.
  268. */
  269. int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
  270. {
  271. struct at91_port *at91_port = at91_pio_get_port(port);
  272. u32 mask;
  273. if (at91_port && (pin < 32)) {
  274. mask = 1 << pin;
  275. if (is_on)
  276. writel(mask, &at91_port->mder);
  277. else
  278. writel(mask, &at91_port->mddr);
  279. }
  280. return 0;
  281. }
  282. /*
  283. * assuming the pin is muxed as a gpio output, set its value.
  284. */
  285. int at91_set_pio_value(unsigned port, unsigned pin, int value)
  286. {
  287. struct at91_port *at91_port = at91_pio_get_port(port);
  288. u32 mask;
  289. if (at91_port && (pin < 32)) {
  290. mask = 1 << pin;
  291. if (value)
  292. writel(mask, &at91_port->sodr);
  293. else
  294. writel(mask, &at91_port->codr);
  295. }
  296. return 0;
  297. }
  298. /*
  299. * read the pin's value (works even if it's not muxed as a gpio).
  300. */
  301. int at91_get_pio_value(unsigned port, unsigned pin)
  302. {
  303. struct at91_port *at91_port = at91_pio_get_port(port);
  304. u32 pdsr = 0, mask;
  305. if (at91_port && (pin < 32)) {
  306. mask = 1 << pin;
  307. pdsr = readl(&at91_port->pdsr) & mask;
  308. }
  309. return pdsr != 0;
  310. }
  311. /* Common GPIO API */
  312. int gpio_request(unsigned gpio, const char *label)
  313. {
  314. return 0;
  315. }
  316. int gpio_free(unsigned gpio)
  317. {
  318. return 0;
  319. }
  320. int gpio_direction_input(unsigned gpio)
  321. {
  322. at91_set_pio_input(at91_gpio_to_port(gpio),
  323. at91_gpio_to_pin(gpio), 0);
  324. return 0;
  325. }
  326. int gpio_direction_output(unsigned gpio, int value)
  327. {
  328. at91_set_pio_output(at91_gpio_to_port(gpio),
  329. at91_gpio_to_pin(gpio), value);
  330. return 0;
  331. }
  332. int gpio_get_value(unsigned gpio)
  333. {
  334. return at91_get_pio_value(at91_gpio_to_port(gpio),
  335. at91_gpio_to_pin(gpio));
  336. }
  337. int gpio_set_value(unsigned gpio, int value)
  338. {
  339. at91_set_pio_value(at91_gpio_to_port(gpio),
  340. at91_gpio_to_pin(gpio), value);
  341. return 0;
  342. }