pinctrl_pic32.c 9.3 KB

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