pinctrl-at91.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Atmel PIO pinctrl driver
  4. *
  5. * Copyright (C) 2016 Atmel Corporation
  6. * Wenyou.Yang <wenyou.yang@atmel.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <dm/pinctrl.h>
  11. #include <asm/hardware.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <mach/at91_pio.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define MAX_GPIO_BANKS 5
  17. #define MAX_NB_GPIO_PER_BANK 32
  18. #define MAX_PINMUX_ENTRIES 200
  19. struct at91_pinctrl_priv {
  20. struct at91_port *reg_base[MAX_GPIO_BANKS];
  21. u32 nbanks;
  22. };
  23. #define PULL_UP BIT(0)
  24. #define MULTI_DRIVE BIT(1)
  25. #define DEGLITCH BIT(2)
  26. #define PULL_DOWN BIT(3)
  27. #define DIS_SCHMIT BIT(4)
  28. #define DRIVE_STRENGTH_SHIFT 5
  29. #define DRIVE_STRENGTH_MASK 0x3
  30. #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
  31. #define OUTPUT BIT(7)
  32. #define OUTPUT_VAL_SHIFT 8
  33. #define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT)
  34. #define DEBOUNCE BIT(16)
  35. #define DEBOUNCE_VAL_SHIFT 17
  36. #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
  37. /**
  38. * These defines will translated the dt binding settings to our internal
  39. * settings. They are not necessarily the same value as the register setting.
  40. * The actual drive strength current of low, medium and high must be looked up
  41. * from the corresponding device datasheet. This value is different for pins
  42. * that are even in the same banks. It is also dependent on VCC.
  43. * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
  44. * strength when there is no dt config for it.
  45. */
  46. #define DRIVE_STRENGTH_DEFAULT (0 << DRIVE_STRENGTH_SHIFT)
  47. #define DRIVE_STRENGTH_LOW (1 << DRIVE_STRENGTH_SHIFT)
  48. #define DRIVE_STRENGTH_MED (2 << DRIVE_STRENGTH_SHIFT)
  49. #define DRIVE_STRENGTH_HI (3 << DRIVE_STRENGTH_SHIFT)
  50. enum at91_mux {
  51. AT91_MUX_GPIO = 0,
  52. AT91_MUX_PERIPH_A = 1,
  53. AT91_MUX_PERIPH_B = 2,
  54. AT91_MUX_PERIPH_C = 3,
  55. AT91_MUX_PERIPH_D = 4,
  56. };
  57. /**
  58. * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
  59. * on new IP with support for periph C and D the way to mux in
  60. * periph A and B has changed
  61. * So provide the right callbacks
  62. * if not present means the IP does not support it
  63. * @mux_A_periph: assign the corresponding pin to the peripheral A function.
  64. * @mux_B_periph: assign the corresponding pin to the peripheral B function.
  65. * @mux_C_periph: assign the corresponding pin to the peripheral C function.
  66. * @mux_D_periph: assign the corresponding pin to the peripheral D function.
  67. * @set_deglitch: enable/disable the deglitch feature.
  68. * @set_debounce: enable/disable the debounce feature.
  69. * @set_pulldown: enable/disable the pulldown feature.
  70. * @disable_schmitt_trig: disable schmitt trigger
  71. */
  72. struct at91_pinctrl_mux_ops {
  73. void (*mux_A_periph)(struct at91_port *pio, u32 mask);
  74. void (*mux_B_periph)(struct at91_port *pio, u32 mask);
  75. void (*mux_C_periph)(struct at91_port *pio, u32 mask);
  76. void (*mux_D_periph)(struct at91_port *pio, u32 mask);
  77. void (*set_deglitch)(struct at91_port *pio, u32 mask, bool is_on);
  78. void (*set_debounce)(struct at91_port *pio, u32 mask, bool is_on,
  79. u32 div);
  80. void (*set_pulldown)(struct at91_port *pio, u32 mask, bool is_on);
  81. void (*disable_schmitt_trig)(struct at91_port *pio, u32 mask);
  82. void (*set_drivestrength)(struct at91_port *pio, u32 pin,
  83. u32 strength);
  84. };
  85. static u32 two_bit_pin_value_shift_amount(u32 pin)
  86. {
  87. /* return the shift value for a pin for "two bit" per pin registers,
  88. * i.e. drive strength */
  89. return 2 * ((pin >= MAX_NB_GPIO_PER_BANK/2)
  90. ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
  91. }
  92. static void at91_mux_disable_interrupt(struct at91_port *pio, u32 mask)
  93. {
  94. writel(mask, &pio->idr);
  95. }
  96. static void at91_mux_set_pullup(struct at91_port *pio, u32 mask, bool on)
  97. {
  98. if (on)
  99. writel(mask, &pio->mux.pio3.ppddr);
  100. writel(mask, (on ? &pio->puer : &pio->pudr));
  101. }
  102. static void at91_mux_set_output(struct at91_port *pio, unsigned mask,
  103. bool is_on, bool val)
  104. {
  105. writel(mask, (val ? &pio->sodr : &pio->codr));
  106. writel(mask, (is_on ? &pio->oer : &pio->odr));
  107. }
  108. static void at91_mux_set_multidrive(struct at91_port *pio, u32 mask, bool on)
  109. {
  110. writel(mask, (on ? &pio->mder : &pio->mddr));
  111. }
  112. static void at91_mux_set_A_periph(struct at91_port *pio, u32 mask)
  113. {
  114. writel(mask, &pio->mux.pio2.asr);
  115. }
  116. static void at91_mux_set_B_periph(struct at91_port *pio, u32 mask)
  117. {
  118. writel(mask, &pio->mux.pio2.bsr);
  119. }
  120. static void at91_mux_pio3_set_A_periph(struct at91_port *pio, u32 mask)
  121. {
  122. writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1);
  123. writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2);
  124. }
  125. static void at91_mux_pio3_set_B_periph(struct at91_port *pio, u32 mask)
  126. {
  127. writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1);
  128. writel(readl(&pio->mux.pio3.abcdsr2) & ~mask, &pio->mux.pio3.abcdsr2);
  129. }
  130. static void at91_mux_pio3_set_C_periph(struct at91_port *pio, u32 mask)
  131. {
  132. writel(readl(&pio->mux.pio3.abcdsr1) & ~mask, &pio->mux.pio3.abcdsr1);
  133. writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2);
  134. }
  135. static void at91_mux_pio3_set_D_periph(struct at91_port *pio, u32 mask)
  136. {
  137. writel(readl(&pio->mux.pio3.abcdsr1) | mask, &pio->mux.pio3.abcdsr1);
  138. writel(readl(&pio->mux.pio3.abcdsr2) | mask, &pio->mux.pio3.abcdsr2);
  139. }
  140. static void at91_mux_set_deglitch(struct at91_port *pio, u32 mask, bool is_on)
  141. {
  142. writel(mask, (is_on ? &pio->ifer : &pio->ifdr));
  143. }
  144. static void at91_mux_pio3_set_deglitch(struct at91_port *pio,
  145. u32 mask, bool is_on)
  146. {
  147. if (is_on)
  148. writel(mask, &pio->mux.pio3.ifscdr);
  149. at91_mux_set_deglitch(pio, mask, is_on);
  150. }
  151. static void at91_mux_pio3_set_debounce(struct at91_port *pio, u32 mask,
  152. bool is_on, u32 div)
  153. {
  154. if (is_on) {
  155. writel(mask, &pio->mux.pio3.ifscer);
  156. writel(div & PIO_SCDR_DIV, &pio->mux.pio3.scdr);
  157. writel(mask, &pio->ifer);
  158. } else {
  159. writel(mask, &pio->mux.pio3.ifscdr);
  160. }
  161. }
  162. static void at91_mux_pio3_set_pulldown(struct at91_port *pio,
  163. u32 mask, bool is_on)
  164. {
  165. if (is_on)
  166. writel(mask, &pio->pudr);
  167. writel(mask, (is_on ? &pio->mux.pio3.ppder : &pio->mux.pio3.ppddr));
  168. }
  169. static void at91_mux_pio3_disable_schmitt_trig(struct at91_port *pio,
  170. u32 mask)
  171. {
  172. writel(readl(&pio->schmitt) | mask, &pio->schmitt);
  173. }
  174. static void set_drive_strength(void *reg, u32 pin, u32 strength)
  175. {
  176. u32 shift = two_bit_pin_value_shift_amount(pin);
  177. clrsetbits_le32(reg, DRIVE_STRENGTH_MASK << shift, strength << shift);
  178. }
  179. static void at91_mux_sama5d3_set_drivestrength(struct at91_port *pio,
  180. u32 pin, u32 setting)
  181. {
  182. void *reg;
  183. reg = &pio->driver12;
  184. if (pin >= MAX_NB_GPIO_PER_BANK / 2)
  185. reg = &pio->driver2;
  186. /* do nothing if setting is zero */
  187. if (!setting)
  188. return;
  189. /* strength is 1 to 1 with setting for SAMA5 */
  190. set_drive_strength(reg, pin, setting);
  191. }
  192. static void at91_mux_sam9x5_set_drivestrength(struct at91_port *pio,
  193. u32 pin, u32 setting)
  194. {
  195. void *reg;
  196. reg = &pio->driver1;
  197. if (pin >= MAX_NB_GPIO_PER_BANK / 2)
  198. reg = &pio->driver12;
  199. /* do nothing if setting is zero */
  200. if (!setting)
  201. return;
  202. /* strength is inverse on SAM9x5s with our defines
  203. * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
  204. setting = DRIVE_STRENGTH_HI - setting;
  205. set_drive_strength(reg, pin, setting);
  206. }
  207. static struct at91_pinctrl_mux_ops at91rm9200_ops = {
  208. .mux_A_periph = at91_mux_set_A_periph,
  209. .mux_B_periph = at91_mux_set_B_periph,
  210. .set_deglitch = at91_mux_set_deglitch,
  211. };
  212. static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
  213. .mux_A_periph = at91_mux_pio3_set_A_periph,
  214. .mux_B_periph = at91_mux_pio3_set_B_periph,
  215. .mux_C_periph = at91_mux_pio3_set_C_periph,
  216. .mux_D_periph = at91_mux_pio3_set_D_periph,
  217. .set_deglitch = at91_mux_pio3_set_deglitch,
  218. .set_debounce = at91_mux_pio3_set_debounce,
  219. .set_pulldown = at91_mux_pio3_set_pulldown,
  220. .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
  221. .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
  222. };
  223. static struct at91_pinctrl_mux_ops sama5d3_ops = {
  224. .mux_A_periph = at91_mux_pio3_set_A_periph,
  225. .mux_B_periph = at91_mux_pio3_set_B_periph,
  226. .mux_C_periph = at91_mux_pio3_set_C_periph,
  227. .mux_D_periph = at91_mux_pio3_set_D_periph,
  228. .set_deglitch = at91_mux_pio3_set_deglitch,
  229. .set_debounce = at91_mux_pio3_set_debounce,
  230. .set_pulldown = at91_mux_pio3_set_pulldown,
  231. .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
  232. .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
  233. };
  234. static void at91_mux_gpio_disable(struct at91_port *pio, u32 mask)
  235. {
  236. writel(mask, &pio->pdr);
  237. }
  238. static void at91_mux_gpio_enable(struct at91_port *pio, u32 mask, bool input)
  239. {
  240. writel(mask, &pio->per);
  241. writel(mask, (input ? &pio->odr : &pio->oer));
  242. }
  243. static int at91_pmx_set(struct at91_pinctrl_mux_ops *ops,
  244. struct at91_port *pio, u32 mask, enum at91_mux mux)
  245. {
  246. at91_mux_disable_interrupt(pio, mask);
  247. switch (mux) {
  248. case AT91_MUX_GPIO:
  249. at91_mux_gpio_enable(pio, mask, 1);
  250. break;
  251. case AT91_MUX_PERIPH_A:
  252. ops->mux_A_periph(pio, mask);
  253. break;
  254. case AT91_MUX_PERIPH_B:
  255. ops->mux_B_periph(pio, mask);
  256. break;
  257. case AT91_MUX_PERIPH_C:
  258. if (!ops->mux_C_periph)
  259. return -EINVAL;
  260. ops->mux_C_periph(pio, mask);
  261. break;
  262. case AT91_MUX_PERIPH_D:
  263. if (!ops->mux_D_periph)
  264. return -EINVAL;
  265. ops->mux_D_periph(pio, mask);
  266. break;
  267. }
  268. if (mux)
  269. at91_mux_gpio_disable(pio, mask);
  270. return 0;
  271. }
  272. static int at91_pinconf_set(struct at91_pinctrl_mux_ops *ops,
  273. struct at91_port *pio, u32 pin, u32 config)
  274. {
  275. u32 mask = BIT(pin);
  276. if ((config & PULL_UP) && (config & PULL_DOWN))
  277. return -EINVAL;
  278. at91_mux_set_output(pio, mask, config & OUTPUT,
  279. (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
  280. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  281. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  282. if (ops->set_deglitch)
  283. ops->set_deglitch(pio, mask, config & DEGLITCH);
  284. if (ops->set_debounce)
  285. ops->set_debounce(pio, mask, config & DEBOUNCE,
  286. (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
  287. if (ops->set_pulldown)
  288. ops->set_pulldown(pio, mask, config & PULL_DOWN);
  289. if (ops->disable_schmitt_trig && config & DIS_SCHMIT)
  290. ops->disable_schmitt_trig(pio, mask);
  291. if (ops->set_drivestrength)
  292. ops->set_drivestrength(pio, pin,
  293. (config & DRIVE_STRENGTH) >> DRIVE_STRENGTH_SHIFT);
  294. return 0;
  295. }
  296. static int at91_pin_check_config(struct udevice *dev, u32 bank, u32 pin)
  297. {
  298. struct at91_pinctrl_priv *priv = dev_get_priv(dev);
  299. if (bank >= priv->nbanks) {
  300. debug("pin conf bank %d >= nbanks %d\n", bank, priv->nbanks);
  301. return -EINVAL;
  302. }
  303. if (pin >= MAX_NB_GPIO_PER_BANK) {
  304. debug("pin conf pin %d >= %d\n", pin, MAX_NB_GPIO_PER_BANK);
  305. return -EINVAL;
  306. }
  307. return 0;
  308. }
  309. static int at91_pinctrl_set_state(struct udevice *dev, struct udevice *config)
  310. {
  311. struct at91_pinctrl_priv *priv = dev_get_priv(dev);
  312. const void *blob = gd->fdt_blob;
  313. int node = dev_of_offset(config);
  314. u32 cells[MAX_PINMUX_ENTRIES];
  315. const u32 *list = cells;
  316. u32 bank, pin;
  317. u32 conf, mask, count, i;
  318. int size;
  319. int ret;
  320. enum at91_mux mux;
  321. struct at91_port *pio;
  322. struct at91_pinctrl_mux_ops *ops =
  323. (struct at91_pinctrl_mux_ops *)dev_get_driver_data(dev);
  324. /*
  325. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  326. * do sanity check and calculate pins number
  327. */
  328. size = fdtdec_get_int_array_count(blob, node, "atmel,pins",
  329. cells, ARRAY_SIZE(cells));
  330. /* we do not check return since it's safe node passed down */
  331. count = size >> 2;
  332. if (!count)
  333. return -EINVAL;
  334. for (i = 0; i < count; i++) {
  335. bank = *list++;
  336. pin = *list++;
  337. mux = *list++;
  338. conf = *list++;
  339. ret = at91_pin_check_config(dev, bank, pin);
  340. if (ret)
  341. return ret;
  342. pio = priv->reg_base[bank];
  343. mask = BIT(pin);
  344. ret = at91_pmx_set(ops, pio, mask, mux);
  345. if (ret)
  346. return ret;
  347. ret = at91_pinconf_set(ops, pio, pin, conf);
  348. if (ret)
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. const struct pinctrl_ops at91_pinctrl_ops = {
  354. .set_state = at91_pinctrl_set_state,
  355. };
  356. static int at91_pinctrl_probe(struct udevice *dev)
  357. {
  358. struct at91_pinctrl_priv *priv = dev_get_priv(dev);
  359. fdt_addr_t addr_base;
  360. int index;
  361. for (index = 0; index < MAX_GPIO_BANKS; index++) {
  362. addr_base = devfdt_get_addr_index(dev, index);
  363. if (addr_base == FDT_ADDR_T_NONE)
  364. break;
  365. priv->reg_base[index] = (struct at91_port *)addr_base;
  366. }
  367. priv->nbanks = index;
  368. return 0;
  369. }
  370. static const struct udevice_id at91_pinctrl_match[] = {
  371. { .compatible = "atmel,sama5d3-pinctrl", .data = (ulong)&sama5d3_ops },
  372. { .compatible = "atmel,at91sam9x5-pinctrl", .data = (ulong)&at91sam9x5_ops },
  373. { .compatible = "atmel,at91rm9200-pinctrl", .data = (ulong)&at91rm9200_ops },
  374. {}
  375. };
  376. U_BOOT_DRIVER(at91_pinctrl) = {
  377. .name = "pinctrl_at91",
  378. .id = UCLASS_PINCTRL,
  379. .of_match = at91_pinctrl_match,
  380. .probe = at91_pinctrl_probe,
  381. .priv_auto_alloc_size = sizeof(struct at91_pinctrl_priv),
  382. .ops = &at91_pinctrl_ops,
  383. };