pinctrl_pic32.c 8.3 KB

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