at91_gpio.c 12 KB

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