at91_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 <dm.h>
  13. #include <asm/io.h>
  14. #include <linux/sizes.h>
  15. #include <asm/gpio.h>
  16. #include <asm/arch/hardware.h>
  17. #include <asm/arch/at91_pio.h>
  18. #define GPIO_PER_BANK 32
  19. static struct at91_port *at91_pio_get_port(unsigned port)
  20. {
  21. switch (port) {
  22. case AT91_PIO_PORTA:
  23. return (struct at91_port *)ATMEL_BASE_PIOA;
  24. case AT91_PIO_PORTB:
  25. return (struct at91_port *)ATMEL_BASE_PIOB;
  26. case AT91_PIO_PORTC:
  27. return (struct at91_port *)ATMEL_BASE_PIOC;
  28. #if (ATMEL_PIO_PORTS > 3)
  29. case AT91_PIO_PORTD:
  30. return (struct at91_port *)ATMEL_BASE_PIOD;
  31. #if (ATMEL_PIO_PORTS > 4)
  32. case AT91_PIO_PORTE:
  33. return (struct at91_port *)ATMEL_BASE_PIOE;
  34. #endif
  35. #endif
  36. default:
  37. printf("Error: at91_gpio: Fail to get PIO base!\n");
  38. return NULL;
  39. }
  40. }
  41. static void at91_set_port_pullup(struct at91_port *at91_port, unsigned offset,
  42. int use_pullup)
  43. {
  44. u32 mask;
  45. mask = 1 << offset;
  46. if (use_pullup)
  47. writel(mask, &at91_port->puer);
  48. else
  49. writel(mask, &at91_port->pudr);
  50. writel(mask, &at91_port->per);
  51. }
  52. int at91_set_pio_pullup(unsigned port, unsigned pin, int use_pullup)
  53. {
  54. struct at91_port *at91_port = at91_pio_get_port(port);
  55. if (at91_port && (pin < GPIO_PER_BANK))
  56. at91_set_port_pullup(at91_port, pin, use_pullup);
  57. return 0;
  58. }
  59. /*
  60. * mux the pin to the "GPIO" peripheral role.
  61. */
  62. int at91_set_pio_periph(unsigned port, unsigned pin, int use_pullup)
  63. {
  64. struct at91_port *at91_port = at91_pio_get_port(port);
  65. u32 mask;
  66. if (at91_port && (pin < GPIO_PER_BANK)) {
  67. mask = 1 << pin;
  68. writel(mask, &at91_port->idr);
  69. at91_set_pio_pullup(port, pin, use_pullup);
  70. writel(mask, &at91_port->per);
  71. }
  72. return 0;
  73. }
  74. /*
  75. * mux the pin to the "A" internal peripheral role.
  76. */
  77. int at91_set_a_periph(unsigned port, unsigned pin, int use_pullup)
  78. {
  79. struct at91_port *at91_port = at91_pio_get_port(port);
  80. u32 mask;
  81. if (at91_port && (pin < GPIO_PER_BANK)) {
  82. mask = 1 << pin;
  83. writel(mask, &at91_port->idr);
  84. at91_set_pio_pullup(port, pin, use_pullup);
  85. #if defined(CPU_HAS_PIO3)
  86. writel(readl(&at91_port->abcdsr1) & ~mask,
  87. &at91_port->abcdsr1);
  88. writel(readl(&at91_port->abcdsr2) & ~mask,
  89. &at91_port->abcdsr2);
  90. #else
  91. writel(mask, &at91_port->asr);
  92. #endif
  93. writel(mask, &at91_port->pdr);
  94. }
  95. return 0;
  96. }
  97. /*
  98. * mux the pin to the "B" internal peripheral role.
  99. */
  100. int at91_set_b_periph(unsigned port, unsigned pin, int use_pullup)
  101. {
  102. struct at91_port *at91_port = at91_pio_get_port(port);
  103. u32 mask;
  104. if (at91_port && (pin < GPIO_PER_BANK)) {
  105. mask = 1 << pin;
  106. writel(mask, &at91_port->idr);
  107. at91_set_pio_pullup(port, pin, use_pullup);
  108. #if defined(CPU_HAS_PIO3)
  109. writel(readl(&at91_port->abcdsr1) | mask,
  110. &at91_port->abcdsr1);
  111. writel(readl(&at91_port->abcdsr2) & ~mask,
  112. &at91_port->abcdsr2);
  113. #else
  114. writel(mask, &at91_port->bsr);
  115. #endif
  116. writel(mask, &at91_port->pdr);
  117. }
  118. return 0;
  119. }
  120. #if defined(CPU_HAS_PIO3)
  121. /*
  122. * mux the pin to the "C" internal peripheral role.
  123. */
  124. int at91_set_c_periph(unsigned port, unsigned pin, int use_pullup)
  125. {
  126. struct at91_port *at91_port = at91_pio_get_port(port);
  127. u32 mask;
  128. if (at91_port && (pin < GPIO_PER_BANK)) {
  129. mask = 1 << pin;
  130. writel(mask, &at91_port->idr);
  131. at91_set_pio_pullup(port, pin, use_pullup);
  132. writel(readl(&at91_port->abcdsr1) & ~mask,
  133. &at91_port->abcdsr1);
  134. writel(readl(&at91_port->abcdsr2) | mask,
  135. &at91_port->abcdsr2);
  136. writel(mask, &at91_port->pdr);
  137. }
  138. return 0;
  139. }
  140. /*
  141. * mux the pin to the "D" internal peripheral role.
  142. */
  143. int at91_set_d_periph(unsigned port, unsigned pin, int use_pullup)
  144. {
  145. struct at91_port *at91_port = at91_pio_get_port(port);
  146. u32 mask;
  147. if (at91_port && (pin < GPIO_PER_BANK)) {
  148. mask = 1 << pin;
  149. writel(mask, &at91_port->idr);
  150. at91_set_pio_pullup(port, pin, use_pullup);
  151. writel(readl(&at91_port->abcdsr1) | mask,
  152. &at91_port->abcdsr1);
  153. writel(readl(&at91_port->abcdsr2) | mask,
  154. &at91_port->abcdsr2);
  155. writel(mask, &at91_port->pdr);
  156. }
  157. return 0;
  158. }
  159. #endif
  160. #ifdef CONFIG_DM_GPIO
  161. static bool at91_get_port_output(struct at91_port *at91_port, int offset)
  162. {
  163. u32 mask, val;
  164. mask = 1 << offset;
  165. val = readl(&at91_port->osr);
  166. return val & mask;
  167. }
  168. #endif
  169. static void at91_set_port_input(struct at91_port *at91_port, int offset,
  170. int use_pullup)
  171. {
  172. u32 mask;
  173. mask = 1 << offset;
  174. writel(mask, &at91_port->idr);
  175. at91_set_port_pullup(at91_port, offset, use_pullup);
  176. writel(mask, &at91_port->odr);
  177. writel(mask, &at91_port->per);
  178. }
  179. /*
  180. * mux the pin to the gpio controller (instead of "A" or "B" peripheral), and
  181. * configure it for an input.
  182. */
  183. int at91_set_pio_input(unsigned port, u32 pin, int use_pullup)
  184. {
  185. struct at91_port *at91_port = at91_pio_get_port(port);
  186. if (at91_port && (pin < GPIO_PER_BANK))
  187. at91_set_port_input(at91_port, pin, use_pullup);
  188. return 0;
  189. }
  190. static void at91_set_port_output(struct at91_port *at91_port, int offset,
  191. int value)
  192. {
  193. u32 mask;
  194. mask = 1 << offset;
  195. writel(mask, &at91_port->idr);
  196. writel(mask, &at91_port->pudr);
  197. if (value)
  198. writel(mask, &at91_port->sodr);
  199. else
  200. writel(mask, &at91_port->codr);
  201. writel(mask, &at91_port->oer);
  202. writel(mask, &at91_port->per);
  203. }
  204. /*
  205. * mux the pin to the gpio controller (instead of "A" or "B" peripheral),
  206. * and configure it for an output.
  207. */
  208. int at91_set_pio_output(unsigned port, u32 pin, int value)
  209. {
  210. struct at91_port *at91_port = at91_pio_get_port(port);
  211. if (at91_port && (pin < GPIO_PER_BANK))
  212. at91_set_port_output(at91_port, pin, value);
  213. return 0;
  214. }
  215. /*
  216. * enable/disable the glitch filter. mostly used with IRQ handling.
  217. */
  218. int at91_set_pio_deglitch(unsigned port, unsigned pin, int is_on)
  219. {
  220. struct at91_port *at91_port = at91_pio_get_port(port);
  221. u32 mask;
  222. if (at91_port && (pin < GPIO_PER_BANK)) {
  223. mask = 1 << pin;
  224. if (is_on) {
  225. #if defined(CPU_HAS_PIO3)
  226. writel(mask, &at91_port->ifscdr);
  227. #endif
  228. writel(mask, &at91_port->ifer);
  229. } else {
  230. writel(mask, &at91_port->ifdr);
  231. }
  232. }
  233. return 0;
  234. }
  235. #if defined(CPU_HAS_PIO3)
  236. /*
  237. * enable/disable the debounce filter.
  238. */
  239. int at91_set_pio_debounce(unsigned port, unsigned pin, int is_on, int div)
  240. {
  241. struct at91_port *at91_port = at91_pio_get_port(port);
  242. u32 mask;
  243. if (at91_port && (pin < GPIO_PER_BANK)) {
  244. mask = 1 << pin;
  245. if (is_on) {
  246. writel(mask, &at91_port->ifscer);
  247. writel(div & PIO_SCDR_DIV, &at91_port->scdr);
  248. writel(mask, &at91_port->ifer);
  249. } else {
  250. writel(mask, &at91_port->ifdr);
  251. }
  252. }
  253. return 0;
  254. }
  255. /*
  256. * enable/disable the pull-down.
  257. * If pull-up already enabled while calling the function, we disable it.
  258. */
  259. int at91_set_pio_pulldown(unsigned port, unsigned pin, int is_on)
  260. {
  261. struct at91_port *at91_port = at91_pio_get_port(port);
  262. u32 mask;
  263. if (at91_port && (pin < GPIO_PER_BANK)) {
  264. mask = 1 << pin;
  265. writel(mask, &at91_port->pudr);
  266. if (is_on)
  267. writel(mask, &at91_port->ppder);
  268. else
  269. writel(mask, &at91_port->ppddr);
  270. }
  271. return 0;
  272. }
  273. /*
  274. * disable Schmitt trigger
  275. */
  276. int at91_set_pio_disable_schmitt_trig(unsigned port, unsigned pin)
  277. {
  278. struct at91_port *at91_port = at91_pio_get_port(port);
  279. u32 mask;
  280. if (at91_port && (pin < GPIO_PER_BANK)) {
  281. mask = 1 << pin;
  282. writel(readl(&at91_port->schmitt) | mask,
  283. &at91_port->schmitt);
  284. }
  285. return 0;
  286. }
  287. #endif
  288. /*
  289. * enable/disable the multi-driver. This is only valid for output and
  290. * allows the output pin to run as an open collector output.
  291. */
  292. int at91_set_pio_multi_drive(unsigned port, unsigned pin, int is_on)
  293. {
  294. struct at91_port *at91_port = at91_pio_get_port(port);
  295. u32 mask;
  296. if (at91_port && (pin < GPIO_PER_BANK)) {
  297. mask = 1 << pin;
  298. if (is_on)
  299. writel(mask, &at91_port->mder);
  300. else
  301. writel(mask, &at91_port->mddr);
  302. }
  303. return 0;
  304. }
  305. static void at91_set_port_value(struct at91_port *at91_port, int offset,
  306. int value)
  307. {
  308. u32 mask;
  309. mask = 1 << offset;
  310. if (value)
  311. writel(mask, &at91_port->sodr);
  312. else
  313. writel(mask, &at91_port->codr);
  314. }
  315. /*
  316. * assuming the pin is muxed as a gpio output, set its value.
  317. */
  318. int at91_set_pio_value(unsigned port, unsigned pin, int value)
  319. {
  320. struct at91_port *at91_port = at91_pio_get_port(port);
  321. if (at91_port && (pin < GPIO_PER_BANK))
  322. at91_set_port_value(at91_port, pin, value);
  323. return 0;
  324. }
  325. static int at91_get_port_value(struct at91_port *at91_port, int offset)
  326. {
  327. u32 pdsr = 0, mask;
  328. mask = 1 << offset;
  329. pdsr = readl(&at91_port->pdsr) & mask;
  330. return pdsr != 0;
  331. }
  332. /*
  333. * read the pin's value (works even if it's not muxed as a gpio).
  334. */
  335. int at91_get_pio_value(unsigned port, unsigned pin)
  336. {
  337. struct at91_port *at91_port = at91_pio_get_port(port);
  338. if (at91_port && (pin < GPIO_PER_BANK))
  339. return at91_get_port_value(at91_port, pin);
  340. return 0;
  341. }
  342. #ifndef CONFIG_DM_GPIO
  343. /* Common GPIO API */
  344. int gpio_request(unsigned gpio, const char *label)
  345. {
  346. return 0;
  347. }
  348. int gpio_free(unsigned gpio)
  349. {
  350. return 0;
  351. }
  352. int gpio_direction_input(unsigned gpio)
  353. {
  354. at91_set_pio_input(at91_gpio_to_port(gpio),
  355. at91_gpio_to_pin(gpio), 0);
  356. return 0;
  357. }
  358. int gpio_direction_output(unsigned gpio, int value)
  359. {
  360. at91_set_pio_output(at91_gpio_to_port(gpio),
  361. at91_gpio_to_pin(gpio), value);
  362. return 0;
  363. }
  364. int gpio_get_value(unsigned gpio)
  365. {
  366. return at91_get_pio_value(at91_gpio_to_port(gpio),
  367. at91_gpio_to_pin(gpio));
  368. }
  369. int gpio_set_value(unsigned gpio, int value)
  370. {
  371. at91_set_pio_value(at91_gpio_to_port(gpio),
  372. at91_gpio_to_pin(gpio), value);
  373. return 0;
  374. }
  375. #endif
  376. #ifdef CONFIG_DM_GPIO
  377. struct at91_port_priv {
  378. struct at91_port *regs;
  379. };
  380. /* set GPIO pin 'gpio' as an input */
  381. static int at91_gpio_direction_input(struct udevice *dev, unsigned offset)
  382. {
  383. struct at91_port_priv *port = dev_get_platdata(dev);
  384. at91_set_port_input(port->regs, offset, 0);
  385. return 0;
  386. }
  387. /* set GPIO pin 'gpio' as an output, with polarity 'value' */
  388. static int at91_gpio_direction_output(struct udevice *dev, unsigned offset,
  389. int value)
  390. {
  391. struct at91_port_priv *port = dev_get_platdata(dev);
  392. at91_set_port_output(port->regs, offset, value);
  393. return 0;
  394. }
  395. /* read GPIO IN value of pin 'gpio' */
  396. static int at91_gpio_get_value(struct udevice *dev, unsigned offset)
  397. {
  398. struct at91_port_priv *port = dev_get_platdata(dev);
  399. return at91_get_port_value(port->regs, offset);
  400. }
  401. /* write GPIO OUT value to pin 'gpio' */
  402. static int at91_gpio_set_value(struct udevice *dev, unsigned offset,
  403. int value)
  404. {
  405. struct at91_port_priv *port = dev_get_platdata(dev);
  406. at91_set_port_value(port->regs, offset, value);
  407. return 0;
  408. }
  409. static int at91_gpio_get_function(struct udevice *dev, unsigned offset)
  410. {
  411. struct at91_port_priv *port = dev_get_platdata(dev);
  412. /* GPIOF_FUNC is not implemented yet */
  413. if (at91_get_port_output(port->regs, offset))
  414. return GPIOF_OUTPUT;
  415. else
  416. return GPIOF_INPUT;
  417. }
  418. static const struct dm_gpio_ops gpio_at91_ops = {
  419. .direction_input = at91_gpio_direction_input,
  420. .direction_output = at91_gpio_direction_output,
  421. .get_value = at91_gpio_get_value,
  422. .set_value = at91_gpio_set_value,
  423. .get_function = at91_gpio_get_function,
  424. };
  425. static int at91_gpio_probe(struct udevice *dev)
  426. {
  427. struct at91_port_priv *port = dev_get_priv(dev);
  428. struct at91_port_platdata *plat = dev_get_platdata(dev);
  429. struct gpio_dev_priv *uc_priv = dev->uclass_priv;
  430. uc_priv->bank_name = plat->bank_name;
  431. uc_priv->gpio_count = GPIO_PER_BANK;
  432. port->regs = (struct at91_port *)plat->base_addr;
  433. return 0;
  434. }
  435. U_BOOT_DRIVER(gpio_at91) = {
  436. .name = "gpio_at91",
  437. .id = UCLASS_GPIO,
  438. .ops = &gpio_at91_ops,
  439. .probe = at91_gpio_probe,
  440. .priv_auto_alloc_size = sizeof(struct at91_port_priv),
  441. };
  442. #endif