pinctrl_pic32.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Pinctrl driver for Microchip PIC32 SoCs
  4. * Copyright (c) 2015 Microchip Technology Inc.
  5. * Written by Purna Chandra Mandal <purna.mandal@microchip.com>
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <asm/io.h>
  11. #include <dm/pinctrl.h>
  12. #include <mach/pic32.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* PIC32 has 10 peripheral ports with 16 pins each.
  15. * Ports are marked PORTA-PORTK or PORT0-PORT9.
  16. */
  17. enum {
  18. PIC32_PORT_A = 0,
  19. PIC32_PORT_B = 1,
  20. PIC32_PORT_C = 2,
  21. PIC32_PORT_D = 3,
  22. PIC32_PORT_E = 4,
  23. PIC32_PORT_F = 5,
  24. PIC32_PORT_G = 6,
  25. PIC32_PORT_H = 7,
  26. PIC32_PORT_J = 8, /* no PORT_I */
  27. PIC32_PORT_K = 9,
  28. PIC32_PINS_PER_PORT = 16,
  29. };
  30. #define PIN_CONFIG_PIC32_DIGITAL (PIN_CONFIG_END + 1)
  31. #define PIN_CONFIG_PIC32_ANALOG (PIN_CONFIG_END + 2)
  32. /* pin configuration descriptor */
  33. struct pic32_pin_config {
  34. u16 port; /* port number */
  35. u16 pin; /* pin number in the port */
  36. u32 config; /* one of PIN_CONFIG_* */
  37. };
  38. #define PIN_CONFIG(_prt, _pin, _cfg) \
  39. {.port = (_prt), .pin = (_pin), .config = (_cfg), }
  40. /* In PIC32 muxing is performed at pin-level through two
  41. * different set of registers - one set for input functions,
  42. * and other for output functions.
  43. * Pin configuration is handled through port register.
  44. */
  45. /* Port control registers */
  46. struct pic32_reg_port {
  47. struct pic32_reg_atomic ansel;
  48. struct pic32_reg_atomic tris;
  49. struct pic32_reg_atomic port;
  50. struct pic32_reg_atomic lat;
  51. struct pic32_reg_atomic odc;
  52. struct pic32_reg_atomic cnpu;
  53. struct pic32_reg_atomic cnpd;
  54. struct pic32_reg_atomic cncon;
  55. struct pic32_reg_atomic unused[8];
  56. };
  57. /* Input function mux registers */
  58. struct pic32_reg_in_mux {
  59. u32 unused0;
  60. u32 int1[4];
  61. u32 unused1;
  62. u32 t2ck[8];
  63. u32 ic1[9];
  64. u32 unused2;
  65. u32 ocfar;
  66. u32 unused3;
  67. u32 u1rx;
  68. u32 u1cts;
  69. u32 u2rx;
  70. u32 u2cts;
  71. u32 u3rx;
  72. u32 u3cts;
  73. u32 u4rx;
  74. u32 u4cts;
  75. u32 u5rx;
  76. u32 u5cts;
  77. u32 u6rx;
  78. u32 u6cts;
  79. u32 unused4;
  80. u32 sdi1;
  81. u32 ss1;
  82. u32 unused5;
  83. u32 sdi2;
  84. u32 ss2;
  85. u32 unused6;
  86. u32 sdi3;
  87. u32 ss3;
  88. u32 unused7;
  89. u32 sdi4;
  90. u32 ss4;
  91. u32 unused8;
  92. u32 sdi5;
  93. u32 ss5;
  94. u32 unused9;
  95. u32 sdi6;
  96. u32 ss6;
  97. u32 c1rx;
  98. u32 c2rx;
  99. u32 refclki1;
  100. u32 refclki2;
  101. u32 refclki3;
  102. u32 refclki4;
  103. };
  104. /* output mux register offset */
  105. #define PPS_OUT(__port, __pin) \
  106. (((__port) * PIC32_PINS_PER_PORT + (__pin)) << 2)
  107. struct pic32_pinctrl_priv {
  108. struct pic32_reg_in_mux *mux_in; /* mux input function */
  109. struct pic32_reg_port *pinconf; /* pin configuration*/
  110. void __iomem *mux_out; /* mux output function */
  111. };
  112. enum {
  113. PERIPH_ID_UART1,
  114. PERIPH_ID_UART2,
  115. PERIPH_ID_ETH,
  116. PERIPH_ID_USB,
  117. PERIPH_ID_SDHCI,
  118. PERIPH_ID_I2C1,
  119. PERIPH_ID_I2C2,
  120. PERIPH_ID_SPI1,
  121. PERIPH_ID_SPI2,
  122. PERIPH_ID_SQI,
  123. };
  124. static int pic32_pinconfig_one(struct pic32_pinctrl_priv *priv,
  125. u32 port_nr, u32 pin, u32 param)
  126. {
  127. struct pic32_reg_port *port;
  128. port = &priv->pinconf[port_nr];
  129. switch (param) {
  130. case PIN_CONFIG_PIC32_DIGITAL:
  131. writel(BIT(pin), &port->ansel.clr);
  132. break;
  133. case PIN_CONFIG_PIC32_ANALOG:
  134. writel(BIT(pin), &port->ansel.set);
  135. break;
  136. case PIN_CONFIG_INPUT_ENABLE:
  137. writel(BIT(pin), &port->tris.set);
  138. break;
  139. case PIN_CONFIG_OUTPUT:
  140. writel(BIT(pin), &port->tris.clr);
  141. break;
  142. case PIN_CONFIG_BIAS_PULL_UP:
  143. writel(BIT(pin), &port->cnpu.set);
  144. break;
  145. case PIN_CONFIG_BIAS_PULL_DOWN:
  146. writel(BIT(pin), &port->cnpd.set);
  147. break;
  148. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  149. writel(BIT(pin), &port->odc.set);
  150. break;
  151. default:
  152. break;
  153. }
  154. return 0;
  155. }
  156. static int pic32_pinconfig_set(struct pic32_pinctrl_priv *priv,
  157. const struct pic32_pin_config *list, int count)
  158. {
  159. int i;
  160. for (i = 0 ; i < count; i++)
  161. pic32_pinconfig_one(priv, list[i].port,
  162. list[i].pin, list[i].config);
  163. return 0;
  164. }
  165. static void pic32_eth_pin_config(struct udevice *dev)
  166. {
  167. struct pic32_pinctrl_priv *priv = dev_get_priv(dev);
  168. const struct pic32_pin_config configs[] = {
  169. /* EMDC - D11 */
  170. PIN_CONFIG(PIC32_PORT_D, 11, PIN_CONFIG_PIC32_DIGITAL),
  171. PIN_CONFIG(PIC32_PORT_D, 11, PIN_CONFIG_OUTPUT),
  172. /* ETXEN */
  173. PIN_CONFIG(PIC32_PORT_D, 6, PIN_CONFIG_PIC32_DIGITAL),
  174. PIN_CONFIG(PIC32_PORT_D, 6, PIN_CONFIG_OUTPUT),
  175. /* ECRSDV */
  176. PIN_CONFIG(PIC32_PORT_H, 13, PIN_CONFIG_PIC32_DIGITAL),
  177. PIN_CONFIG(PIC32_PORT_H, 13, PIN_CONFIG_INPUT_ENABLE),
  178. /* ERXD0 */
  179. PIN_CONFIG(PIC32_PORT_H, 8, PIN_CONFIG_PIC32_DIGITAL),
  180. PIN_CONFIG(PIC32_PORT_H, 8, PIN_CONFIG_INPUT_ENABLE),
  181. PIN_CONFIG(PIC32_PORT_H, 8, PIN_CONFIG_BIAS_PULL_DOWN),
  182. /* ERXD1 */
  183. PIN_CONFIG(PIC32_PORT_H, 5, PIN_CONFIG_PIC32_DIGITAL),
  184. PIN_CONFIG(PIC32_PORT_H, 5, PIN_CONFIG_INPUT_ENABLE),
  185. PIN_CONFIG(PIC32_PORT_H, 5, PIN_CONFIG_BIAS_PULL_DOWN),
  186. /* EREFCLK */
  187. PIN_CONFIG(PIC32_PORT_J, 11, PIN_CONFIG_PIC32_DIGITAL),
  188. PIN_CONFIG(PIC32_PORT_J, 11, PIN_CONFIG_INPUT_ENABLE),
  189. /* ETXD1 */
  190. PIN_CONFIG(PIC32_PORT_J, 9, PIN_CONFIG_PIC32_DIGITAL),
  191. PIN_CONFIG(PIC32_PORT_J, 9, PIN_CONFIG_OUTPUT),
  192. /* ETXD0 */
  193. PIN_CONFIG(PIC32_PORT_J, 8, PIN_CONFIG_PIC32_DIGITAL),
  194. PIN_CONFIG(PIC32_PORT_J, 8, PIN_CONFIG_OUTPUT),
  195. /* EMDIO */
  196. PIN_CONFIG(PIC32_PORT_J, 1, PIN_CONFIG_PIC32_DIGITAL),
  197. PIN_CONFIG(PIC32_PORT_J, 1, PIN_CONFIG_INPUT_ENABLE),
  198. /* ERXERR */
  199. PIN_CONFIG(PIC32_PORT_F, 3, PIN_CONFIG_PIC32_DIGITAL),
  200. PIN_CONFIG(PIC32_PORT_F, 3, PIN_CONFIG_INPUT_ENABLE),
  201. };
  202. pic32_pinconfig_set(priv, configs, ARRAY_SIZE(configs));
  203. }
  204. static int pic32_pinctrl_request(struct udevice *dev, int func, int flags)
  205. {
  206. struct pic32_pinctrl_priv *priv = dev_get_priv(dev);
  207. switch (func) {
  208. case PERIPH_ID_UART2:
  209. /* PPS for U2 RX/TX */
  210. writel(0x02, priv->mux_out + PPS_OUT(PIC32_PORT_G, 9));
  211. writel(0x05, &priv->mux_in->u2rx); /* B0 */
  212. /* set digital mode */
  213. pic32_pinconfig_one(priv, PIC32_PORT_G, 9,
  214. PIN_CONFIG_PIC32_DIGITAL);
  215. pic32_pinconfig_one(priv, PIC32_PORT_B, 0,
  216. PIN_CONFIG_PIC32_DIGITAL);
  217. break;
  218. case PERIPH_ID_ETH:
  219. pic32_eth_pin_config(dev);
  220. break;
  221. default:
  222. debug("%s: unknown-unhandled case\n", __func__);
  223. break;
  224. }
  225. return 0;
  226. }
  227. static int pic32_pinctrl_get_periph_id(struct udevice *dev,
  228. struct udevice *periph)
  229. {
  230. int ret;
  231. u32 cell[2];
  232. ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(periph),
  233. "interrupts", cell, ARRAY_SIZE(cell));
  234. if (ret < 0)
  235. return -EINVAL;
  236. /* interrupt number */
  237. switch (cell[0]) {
  238. case 112 ... 114:
  239. return PERIPH_ID_UART1;
  240. case 145 ... 147:
  241. return PERIPH_ID_UART2;
  242. case 109 ... 111:
  243. return PERIPH_ID_SPI1;
  244. case 142 ... 144:
  245. return PERIPH_ID_SPI2;
  246. case 115 ... 117:
  247. return PERIPH_ID_I2C1;
  248. case 148 ... 150:
  249. return PERIPH_ID_I2C2;
  250. case 132 ... 133:
  251. return PERIPH_ID_USB;
  252. case 169:
  253. return PERIPH_ID_SQI;
  254. case 191:
  255. return PERIPH_ID_SDHCI;
  256. case 153:
  257. return PERIPH_ID_ETH;
  258. default:
  259. break;
  260. }
  261. return -ENOENT;
  262. }
  263. static int pic32_pinctrl_set_state_simple(struct udevice *dev,
  264. struct udevice *periph)
  265. {
  266. int func;
  267. debug("%s: periph %s\n", __func__, periph->name);
  268. func = pic32_pinctrl_get_periph_id(dev, periph);
  269. if (func < 0)
  270. return func;
  271. return pic32_pinctrl_request(dev, func, 0);
  272. }
  273. static struct pinctrl_ops pic32_pinctrl_ops = {
  274. .set_state_simple = pic32_pinctrl_set_state_simple,
  275. .request = pic32_pinctrl_request,
  276. .get_periph_id = pic32_pinctrl_get_periph_id,
  277. };
  278. static int pic32_pinctrl_probe(struct udevice *dev)
  279. {
  280. struct pic32_pinctrl_priv *priv = dev_get_priv(dev);
  281. struct fdt_resource res;
  282. void *fdt = (void *)gd->fdt_blob;
  283. int node = dev_of_offset(dev);
  284. int ret;
  285. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  286. "ppsin", &res);
  287. if (ret < 0) {
  288. printf("pinctrl: resource \"ppsin\" not found\n");
  289. return ret;
  290. }
  291. priv->mux_in = ioremap(res.start, fdt_resource_size(&res));
  292. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  293. "ppsout", &res);
  294. if (ret < 0) {
  295. printf("pinctrl: resource \"ppsout\" not found\n");
  296. return ret;
  297. }
  298. priv->mux_out = ioremap(res.start, fdt_resource_size(&res));
  299. ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
  300. "port", &res);
  301. if (ret < 0) {
  302. printf("pinctrl: resource \"port\" not found\n");
  303. return ret;
  304. }
  305. priv->pinconf = ioremap(res.start, fdt_resource_size(&res));
  306. return 0;
  307. }
  308. static const struct udevice_id pic32_pinctrl_ids[] = {
  309. { .compatible = "microchip,pic32mzda-pinctrl" },
  310. { }
  311. };
  312. U_BOOT_DRIVER(pinctrl_pic32) = {
  313. .name = "pinctrl_pic32",
  314. .id = UCLASS_PINCTRL,
  315. .of_match = pic32_pinctrl_ids,
  316. .ops = &pic32_pinctrl_ops,
  317. .probe = pic32_pinctrl_probe,
  318. .bind = dm_scan_fdt_dev,
  319. .priv_auto_alloc_size = sizeof(struct pic32_pinctrl_priv),
  320. };