pfc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Pin Control driver for SuperH Pin Function Controller.
  4. *
  5. * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
  6. *
  7. * Copyright (C) 2008 Magnus Damm
  8. * Copyright (C) 2009 - 2012 Paul Mundt
  9. * Copyright (C) 2017 Marek Vasut
  10. */
  11. #define DRV_NAME "sh-pfc"
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <dm/device_compat.h>
  16. #include <dm/devres.h>
  17. #include <dm/pinctrl.h>
  18. #include <linux/bitops.h>
  19. #include <linux/bug.h>
  20. #include <linux/io.h>
  21. #include <linux/sizes.h>
  22. #include "sh_pfc.h"
  23. enum sh_pfc_model {
  24. SH_PFC_R8A7790 = 0,
  25. SH_PFC_R8A7791,
  26. SH_PFC_R8A7792,
  27. SH_PFC_R8A7793,
  28. SH_PFC_R8A7794,
  29. SH_PFC_R8A7795,
  30. SH_PFC_R8A77960,
  31. SH_PFC_R8A77961,
  32. SH_PFC_R8A774A1,
  33. SH_PFC_R8A774B1,
  34. SH_PFC_R8A774C0,
  35. SH_PFC_R8A774E1,
  36. SH_PFC_R8A77965,
  37. SH_PFC_R8A77970,
  38. SH_PFC_R8A77980,
  39. SH_PFC_R8A77990,
  40. SH_PFC_R8A77995,
  41. SH_PFC_R8A779A0,
  42. SH_PFC_R8A779F0,
  43. SH_PFC_R8A779G0,
  44. };
  45. struct sh_pfc_pin_config {
  46. u32 type;
  47. const char *name;
  48. };
  49. struct sh_pfc_pinctrl {
  50. struct sh_pfc *pfc;
  51. struct sh_pfc_pin_config *configs;
  52. };
  53. struct sh_pfc_pin_range {
  54. u16 start;
  55. u16 end;
  56. };
  57. struct sh_pfc_pinctrl_priv {
  58. struct sh_pfc pfc;
  59. struct sh_pfc_pinctrl pmx;
  60. };
  61. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  62. {
  63. unsigned int offset;
  64. unsigned int i;
  65. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  66. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  67. if (pin <= range->end)
  68. return pin >= range->start
  69. ? offset + pin - range->start : -1;
  70. offset += range->end - range->start + 1;
  71. }
  72. return -EINVAL;
  73. }
  74. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  75. {
  76. if (enum_id < r->begin)
  77. return 0;
  78. if (enum_id > r->end)
  79. return 0;
  80. return 1;
  81. }
  82. u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
  83. {
  84. switch (reg_width) {
  85. case 8:
  86. return readb(mapped_reg);
  87. case 16:
  88. return readw(mapped_reg);
  89. case 32:
  90. return readl(mapped_reg);
  91. }
  92. BUG();
  93. return 0;
  94. }
  95. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
  96. u32 data)
  97. {
  98. switch (reg_width) {
  99. case 8:
  100. writeb(data, mapped_reg);
  101. return;
  102. case 16:
  103. writew(data, mapped_reg);
  104. return;
  105. case 32:
  106. writel(data, mapped_reg);
  107. return;
  108. }
  109. BUG();
  110. }
  111. u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
  112. {
  113. return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
  114. }
  115. static void sh_pfc_unlock_reg(struct sh_pfc *pfc, u32 reg, u32 data)
  116. {
  117. u32 unlock;
  118. if (!pfc->info->unlock_reg)
  119. return;
  120. if (pfc->info->unlock_reg >= 0x80000000UL)
  121. unlock = pfc->info->unlock_reg;
  122. else
  123. /* unlock_reg is a mask */
  124. unlock = reg & ~pfc->info->unlock_reg;
  125. sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)unlock, 32, ~data);
  126. }
  127. void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
  128. {
  129. sh_pfc_unlock_reg(pfc, reg, data);
  130. sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
  131. }
  132. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  133. const struct pinmux_cfg_reg *crp,
  134. unsigned int in_pos,
  135. void __iomem **mapped_regp, u32 *maskp,
  136. unsigned int *posp)
  137. {
  138. unsigned int k;
  139. *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
  140. if (crp->field_width) {
  141. *maskp = (1 << crp->field_width) - 1;
  142. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  143. } else {
  144. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  145. *posp = crp->reg_width;
  146. for (k = 0; k <= in_pos; k++)
  147. *posp -= abs(crp->var_field_width[k]);
  148. }
  149. }
  150. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  151. const struct pinmux_cfg_reg *crp,
  152. unsigned int field, u32 value)
  153. {
  154. void __iomem *mapped_reg;
  155. unsigned int pos;
  156. u32 mask, data;
  157. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  158. dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
  159. "r_width = %u, f_width = %u\n",
  160. crp->reg, value, field, crp->reg_width, crp->field_width);
  161. mask = ~(mask << pos);
  162. value = value << pos;
  163. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  164. data &= mask;
  165. data |= value;
  166. sh_pfc_unlock_reg(pfc, crp->reg, data);
  167. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  168. }
  169. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  170. const struct pinmux_cfg_reg **crp,
  171. unsigned int *fieldp, u32 *valuep)
  172. {
  173. unsigned int k = 0;
  174. while (1) {
  175. const struct pinmux_cfg_reg *config_reg =
  176. pfc->info->cfg_regs + k;
  177. unsigned int r_width = config_reg->reg_width;
  178. unsigned int f_width = config_reg->field_width;
  179. unsigned int curr_width;
  180. unsigned int bit_pos;
  181. unsigned int pos = 0;
  182. unsigned int m = 0;
  183. if (!r_width)
  184. break;
  185. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width, m++) {
  186. u32 ncomb;
  187. u32 n;
  188. if (f_width) {
  189. curr_width = f_width;
  190. } else {
  191. curr_width = abs(config_reg->var_field_width[m]);
  192. if (config_reg->var_field_width[m] < 0)
  193. continue;
  194. }
  195. ncomb = 1 << curr_width;
  196. for (n = 0; n < ncomb; n++) {
  197. if (config_reg->enum_ids[pos + n] == enum_id) {
  198. *crp = config_reg;
  199. *fieldp = m;
  200. *valuep = n;
  201. return 0;
  202. }
  203. }
  204. pos += ncomb;
  205. }
  206. k++;
  207. }
  208. return -EINVAL;
  209. }
  210. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  211. u16 *enum_idp)
  212. {
  213. const u16 *data = pfc->info->pinmux_data;
  214. unsigned int k;
  215. if (pos) {
  216. *enum_idp = data[pos + 1];
  217. return pos + 1;
  218. }
  219. for (k = 0; k < pfc->info->pinmux_data_size; k++) {
  220. if (data[k] == mark) {
  221. *enum_idp = data[k + 1];
  222. return k + 1;
  223. }
  224. }
  225. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  226. mark);
  227. return -EINVAL;
  228. }
  229. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  230. {
  231. const struct pinmux_range *range;
  232. int pos = 0;
  233. switch (pinmux_type) {
  234. case PINMUX_TYPE_GPIO:
  235. case PINMUX_TYPE_FUNCTION:
  236. range = NULL;
  237. break;
  238. case PINMUX_TYPE_OUTPUT:
  239. range = &pfc->info->output;
  240. break;
  241. case PINMUX_TYPE_INPUT:
  242. range = &pfc->info->input;
  243. break;
  244. default:
  245. return -EINVAL;
  246. }
  247. /* Iterate over all the configuration fields we need to update. */
  248. while (1) {
  249. const struct pinmux_cfg_reg *cr;
  250. unsigned int field;
  251. u16 enum_id;
  252. u32 value;
  253. int in_range;
  254. int ret;
  255. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  256. if (pos < 0)
  257. return pos;
  258. if (!enum_id)
  259. break;
  260. /* Check if the configuration field selects a function. If it
  261. * doesn't, skip the field if it's not applicable to the
  262. * requested pinmux type.
  263. */
  264. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  265. if (!in_range) {
  266. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  267. /* Functions are allowed to modify all
  268. * fields.
  269. */
  270. in_range = 1;
  271. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  272. /* Input/output types can only modify fields
  273. * that correspond to their respective ranges.
  274. */
  275. in_range = sh_pfc_enum_in_range(enum_id, range);
  276. /*
  277. * special case pass through for fixed
  278. * input-only or output-only pins without
  279. * function enum register association.
  280. */
  281. if (in_range && enum_id == range->force)
  282. continue;
  283. }
  284. /* GPIOs are only allowed to modify function fields. */
  285. }
  286. if (!in_range)
  287. continue;
  288. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  289. if (ret < 0)
  290. return ret;
  291. sh_pfc_write_config_reg(pfc, cr, field, value);
  292. }
  293. return 0;
  294. }
  295. const struct pinmux_bias_reg *
  296. rcar_pin_to_bias_reg(const struct sh_pfc_soc_info *info, unsigned int pin,
  297. unsigned int *bit)
  298. {
  299. unsigned int i, j;
  300. for (i = 0; info->bias_regs[i].puen || info->bias_regs[i].pud; i++) {
  301. for (j = 0; j < ARRAY_SIZE(info->bias_regs[i].pins); j++) {
  302. if (info->bias_regs[i].pins[j] == pin) {
  303. *bit = j;
  304. return &info->bias_regs[i];
  305. }
  306. }
  307. }
  308. WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
  309. return NULL;
  310. }
  311. unsigned int rcar_pinmux_get_bias(struct sh_pfc *pfc, unsigned int pin)
  312. {
  313. const struct pinmux_bias_reg *reg;
  314. unsigned int bit;
  315. reg = rcar_pin_to_bias_reg(pfc->info, pin, &bit);
  316. if (!reg)
  317. return PIN_CONFIG_BIAS_DISABLE;
  318. if (reg->puen) {
  319. if (!(sh_pfc_read(pfc, reg->puen) & BIT(bit)))
  320. return PIN_CONFIG_BIAS_DISABLE;
  321. else if (!reg->pud || (sh_pfc_read(pfc, reg->pud) & BIT(bit)))
  322. return PIN_CONFIG_BIAS_PULL_UP;
  323. else
  324. return PIN_CONFIG_BIAS_PULL_DOWN;
  325. } else {
  326. if (sh_pfc_read(pfc, reg->pud) & BIT(bit))
  327. return PIN_CONFIG_BIAS_PULL_DOWN;
  328. else
  329. return PIN_CONFIG_BIAS_DISABLE;
  330. }
  331. }
  332. void rcar_pinmux_set_bias(struct sh_pfc *pfc, unsigned int pin,
  333. unsigned int bias)
  334. {
  335. const struct pinmux_bias_reg *reg;
  336. u32 enable, updown;
  337. unsigned int bit;
  338. reg = rcar_pin_to_bias_reg(pfc->info, pin, &bit);
  339. if (!reg)
  340. return;
  341. if (reg->puen) {
  342. enable = sh_pfc_read(pfc, reg->puen) & ~BIT(bit);
  343. if (bias != PIN_CONFIG_BIAS_DISABLE) {
  344. enable |= BIT(bit);
  345. if (reg->pud) {
  346. updown = sh_pfc_read(pfc, reg->pud) & ~BIT(bit);
  347. if (bias == PIN_CONFIG_BIAS_PULL_UP)
  348. updown |= BIT(bit);
  349. sh_pfc_write(pfc, reg->pud, updown);
  350. }
  351. }
  352. sh_pfc_write(pfc, reg->puen, enable);
  353. } else {
  354. enable = sh_pfc_read(pfc, reg->pud) & ~BIT(bit);
  355. if (bias == PIN_CONFIG_BIAS_PULL_DOWN)
  356. enable |= BIT(bit);
  357. sh_pfc_write(pfc, reg->pud, enable);
  358. }
  359. }
  360. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  361. {
  362. struct sh_pfc_pin_range *range;
  363. unsigned int nr_ranges;
  364. unsigned int i;
  365. if (pfc->info->pins[0].pin == (u16)-1) {
  366. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  367. * in its pin arrays yet. Consider the pin numbers range as
  368. * continuous and allocate a single range.
  369. */
  370. pfc->nr_ranges = 1;
  371. pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
  372. if (pfc->ranges == NULL)
  373. return -ENOMEM;
  374. pfc->ranges->start = 0;
  375. pfc->ranges->end = pfc->info->nr_pins - 1;
  376. pfc->nr_gpio_pins = pfc->info->nr_pins;
  377. return 0;
  378. }
  379. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  380. * be sorted by pin numbers, and pins without a GPIO port must come
  381. * last.
  382. */
  383. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  384. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  385. nr_ranges++;
  386. }
  387. pfc->nr_ranges = nr_ranges;
  388. pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
  389. if (pfc->ranges == NULL)
  390. return -ENOMEM;
  391. range = pfc->ranges;
  392. range->start = pfc->info->pins[0].pin;
  393. for (i = 1; i < pfc->info->nr_pins; ++i) {
  394. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  395. continue;
  396. range->end = pfc->info->pins[i-1].pin;
  397. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  398. pfc->nr_gpio_pins = range->end + 1;
  399. range++;
  400. range->start = pfc->info->pins[i].pin;
  401. }
  402. range->end = pfc->info->pins[i-1].pin;
  403. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  404. pfc->nr_gpio_pins = range->end + 1;
  405. return 0;
  406. }
  407. static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
  408. {
  409. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  410. return priv->pfc.info->nr_pins;
  411. }
  412. static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
  413. unsigned selector)
  414. {
  415. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  416. return priv->pfc.info->pins[selector].name;
  417. }
  418. static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
  419. {
  420. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  421. return priv->pfc.info->nr_groups;
  422. }
  423. static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
  424. unsigned selector)
  425. {
  426. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  427. return priv->pfc.info->groups[selector].name;
  428. }
  429. static int sh_pfc_pinctrl_get_pin_muxing(struct udevice *dev,
  430. unsigned int selector,
  431. char *buf, int size)
  432. {
  433. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  434. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  435. struct sh_pfc *pfc = &priv->pfc;
  436. struct sh_pfc_pin_config *cfg;
  437. const struct sh_pfc_pin *pin;
  438. int idx;
  439. pin = &priv->pfc.info->pins[selector];
  440. if (!pin) {
  441. snprintf(buf, size, "Unknown");
  442. return -EINVAL;
  443. }
  444. idx = sh_pfc_get_pin_index(pfc, pin->pin);
  445. cfg = &pmx->configs[idx];
  446. snprintf(buf, size, "%s", cfg->name);
  447. return 0;
  448. }
  449. static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
  450. {
  451. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  452. return priv->pfc.info->nr_functions;
  453. }
  454. static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
  455. unsigned selector)
  456. {
  457. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  458. return priv->pfc.info->functions[selector].name;
  459. }
  460. static int sh_pfc_gpio_request_enable(struct udevice *dev,
  461. unsigned pin_selector)
  462. {
  463. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  464. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  465. struct sh_pfc *pfc = &priv->pfc;
  466. struct sh_pfc_pin_config *cfg;
  467. const struct sh_pfc_pin *pin = NULL;
  468. int i, ret, idx;
  469. for (i = 0; i < pfc->info->nr_pins; i++) {
  470. if (priv->pfc.info->pins[i].pin != pin_selector)
  471. continue;
  472. pin = &priv->pfc.info->pins[i];
  473. break;
  474. }
  475. if (!pin)
  476. return -EINVAL;
  477. idx = sh_pfc_get_pin_index(pfc, pin->pin);
  478. cfg = &pmx->configs[idx];
  479. if (cfg->type != PINMUX_TYPE_NONE) {
  480. if (!strcmp(cfg->name, pin->name))
  481. return 0;
  482. dev_err(pfc->dev, "Pin already used as %s\n",
  483. cfg->name);
  484. return -EBUSY;
  485. }
  486. ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
  487. if (ret)
  488. return ret;
  489. cfg->type = PINMUX_TYPE_GPIO;
  490. cfg->name = "gpio";
  491. return 0;
  492. }
  493. static int sh_pfc_gpio_disable_free(struct udevice *dev,
  494. unsigned pin_selector)
  495. {
  496. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  497. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  498. struct sh_pfc *pfc = &priv->pfc;
  499. struct sh_pfc_pin_config *cfg;
  500. const struct sh_pfc_pin *pin = NULL;
  501. int i, idx;
  502. for (i = 0; i < pfc->info->nr_pins; i++) {
  503. if (priv->pfc.info->pins[i].pin != pin_selector)
  504. continue;
  505. pin = &priv->pfc.info->pins[i];
  506. break;
  507. }
  508. if (!pin)
  509. return -EINVAL;
  510. idx = sh_pfc_get_pin_index(pfc, pin->pin);
  511. cfg = &pmx->configs[idx];
  512. cfg->type = PINMUX_TYPE_NONE;
  513. cfg->name = "none";
  514. return 0;
  515. }
  516. static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
  517. unsigned func_selector)
  518. {
  519. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  520. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  521. struct sh_pfc *pfc = &priv->pfc;
  522. const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
  523. int idx = sh_pfc_get_pin_index(pfc, pin->pin);
  524. struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
  525. int ret;
  526. if (cfg->type != PINMUX_TYPE_NONE) {
  527. if (!strcmp(cfg->name, pin->name))
  528. return 0;
  529. dev_err(pfc->dev, "Pin already used as %s\n",
  530. cfg->name);
  531. return -EBUSY;
  532. }
  533. ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
  534. if (ret)
  535. return ret;
  536. cfg->type = PINMUX_TYPE_FUNCTION;
  537. cfg->name = "function";
  538. return 0;
  539. }
  540. static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
  541. unsigned func_selector)
  542. {
  543. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  544. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  545. struct sh_pfc *pfc = &priv->pfc;
  546. const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
  547. bool grp_pins_configured = true;
  548. struct sh_pfc_pin_config *cfg;
  549. unsigned int i;
  550. int ret = 0;
  551. int idx;
  552. for (i = 0; i < grp->nr_pins; ++i) {
  553. idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  554. cfg = &pmx->configs[idx];
  555. if (cfg->type != PINMUX_TYPE_NONE) {
  556. if (!strcmp(cfg->name, grp->name))
  557. continue;
  558. dev_err(pfc->dev, "Pin already used as %s\n",
  559. cfg->name);
  560. ret = -EBUSY;
  561. goto done;
  562. } else {
  563. grp_pins_configured = false;
  564. }
  565. }
  566. if (grp_pins_configured)
  567. return 0;
  568. for (i = 0; i < grp->nr_pins; ++i) {
  569. ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
  570. if (ret < 0)
  571. break;
  572. idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
  573. cfg = &pmx->configs[idx];
  574. cfg->type = PINMUX_TYPE_FUNCTION;
  575. cfg->name = priv->pfc.info->groups[group_selector].name;
  576. }
  577. done:
  578. return ret;
  579. }
  580. #if CONFIG_IS_ENABLED(PINCONF)
  581. static const struct pinconf_param sh_pfc_pinconf_params[] = {
  582. { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
  583. { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
  584. { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
  585. { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
  586. { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
  587. };
  588. static void __iomem *
  589. sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
  590. unsigned int *offset, unsigned int *size)
  591. {
  592. const struct pinmux_drive_reg_field *field;
  593. const struct pinmux_drive_reg *reg;
  594. unsigned int i;
  595. for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
  596. for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
  597. field = &reg->fields[i];
  598. if (field->size && field->pin == pin) {
  599. *offset = field->offset;
  600. *size = field->size;
  601. return (void __iomem *)(uintptr_t)reg->reg;
  602. }
  603. }
  604. }
  605. return NULL;
  606. }
  607. static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
  608. unsigned int pin, u16 strength)
  609. {
  610. unsigned int offset;
  611. unsigned int size;
  612. unsigned int step;
  613. void __iomem *reg;
  614. u32 val;
  615. reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
  616. if (!reg)
  617. return -EINVAL;
  618. step = size == 2 ? 6 : 3;
  619. if (strength < step || strength > 24)
  620. return -EINVAL;
  621. /* Convert the value from mA based on a full drive strength value of
  622. * 24mA. We can make the full value configurable later if needed.
  623. */
  624. strength = strength / step - 1;
  625. val = sh_pfc_read_raw_reg(reg, 32);
  626. val &= ~GENMASK(offset + 4 - 1, offset);
  627. val |= strength << offset;
  628. sh_pfc_unlock_reg(pfc, (uintptr_t)reg, val);
  629. sh_pfc_write_raw_reg(reg, 32, val);
  630. return 0;
  631. }
  632. /* Check whether the requested parameter is supported for a pin. */
  633. static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
  634. unsigned int param)
  635. {
  636. int idx = sh_pfc_get_pin_index(pfc, _pin);
  637. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  638. switch (param) {
  639. case PIN_CONFIG_BIAS_DISABLE:
  640. return pin->configs &
  641. (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
  642. case PIN_CONFIG_BIAS_PULL_UP:
  643. return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
  644. case PIN_CONFIG_BIAS_PULL_DOWN:
  645. return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
  646. case PIN_CONFIG_DRIVE_STRENGTH:
  647. return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
  648. case PIN_CONFIG_POWER_SOURCE:
  649. return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
  650. default:
  651. return false;
  652. }
  653. }
  654. static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
  655. unsigned int param, unsigned int arg)
  656. {
  657. struct sh_pfc *pfc = pmx->pfc;
  658. void __iomem *pocctrl;
  659. u32 addr, val;
  660. int bit, ret;
  661. int idx = sh_pfc_get_pin_index(pfc, _pin);
  662. const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
  663. if (!sh_pfc_pinconf_validate(pfc, _pin, param))
  664. return -ENOTSUPP;
  665. switch (param) {
  666. case PIN_CONFIG_BIAS_PULL_UP:
  667. case PIN_CONFIG_BIAS_PULL_DOWN:
  668. case PIN_CONFIG_BIAS_DISABLE:
  669. if (!pfc->info->ops || !pfc->info->ops->set_bias)
  670. return -ENOTSUPP;
  671. pfc->info->ops->set_bias(pfc, _pin, param);
  672. break;
  673. case PIN_CONFIG_DRIVE_STRENGTH:
  674. ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
  675. if (ret < 0)
  676. return ret;
  677. break;
  678. case PIN_CONFIG_POWER_SOURCE:
  679. if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
  680. return -ENOTSUPP;
  681. bit = pfc->info->ops->pin_to_pocctrl(_pin, &addr);
  682. if (bit < 0) {
  683. printf("invalid pin %#x", _pin);
  684. return bit;
  685. }
  686. if (arg != 1800 && arg != 2500 && arg != 3300)
  687. return -EINVAL;
  688. pocctrl = (void __iomem *)(uintptr_t)addr;
  689. val = sh_pfc_read_raw_reg(pocctrl, 32);
  690. if (arg == ((pin->configs & SH_PFC_PIN_VOLTAGE_18_25) ? 2500 : 3300))
  691. val |= BIT(bit);
  692. else
  693. val &= ~BIT(bit);
  694. sh_pfc_unlock_reg(pfc, addr, val);
  695. sh_pfc_write_raw_reg(pocctrl, 32, val);
  696. break;
  697. default:
  698. return -ENOTSUPP;
  699. }
  700. return 0;
  701. }
  702. static int sh_pfc_pinconf_pin_set(struct udevice *dev,
  703. unsigned int pin_selector,
  704. unsigned int param, unsigned int arg)
  705. {
  706. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  707. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  708. struct sh_pfc *pfc = &priv->pfc;
  709. const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
  710. sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
  711. return 0;
  712. }
  713. static int sh_pfc_pinconf_group_set(struct udevice *dev,
  714. unsigned int group_selector,
  715. unsigned int param, unsigned int arg)
  716. {
  717. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  718. struct sh_pfc_pinctrl *pmx = &priv->pmx;
  719. struct sh_pfc *pfc = &priv->pfc;
  720. const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
  721. unsigned int i;
  722. for (i = 0; i < grp->nr_pins; i++)
  723. sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
  724. return 0;
  725. }
  726. #endif
  727. static struct pinctrl_ops sh_pfc_pinctrl_ops = {
  728. .get_pins_count = sh_pfc_pinctrl_get_pins_count,
  729. .get_pin_name = sh_pfc_pinctrl_get_pin_name,
  730. .get_groups_count = sh_pfc_pinctrl_get_groups_count,
  731. .get_group_name = sh_pfc_pinctrl_get_group_name,
  732. .get_pin_muxing = sh_pfc_pinctrl_get_pin_muxing,
  733. .get_functions_count = sh_pfc_pinctrl_get_functions_count,
  734. .get_function_name = sh_pfc_pinctrl_get_function_name,
  735. #if CONFIG_IS_ENABLED(PINCONF)
  736. .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
  737. .pinconf_params = sh_pfc_pinconf_params,
  738. .pinconf_set = sh_pfc_pinconf_pin_set,
  739. .pinconf_group_set = sh_pfc_pinconf_group_set,
  740. #endif
  741. .pinmux_set = sh_pfc_pinctrl_pin_set,
  742. .pinmux_group_set = sh_pfc_pinctrl_group_set,
  743. .set_state = pinctrl_generic_set_state,
  744. .gpio_request_enable = sh_pfc_gpio_request_enable,
  745. .gpio_disable_free = sh_pfc_gpio_disable_free,
  746. };
  747. static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
  748. {
  749. unsigned int i;
  750. /* Allocate and initialize the pins and configs arrays. */
  751. pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
  752. GFP_KERNEL);
  753. if (unlikely(!pmx->configs))
  754. return -ENOMEM;
  755. for (i = 0; i < pfc->info->nr_pins; ++i) {
  756. struct sh_pfc_pin_config *cfg = &pmx->configs[i];
  757. cfg->type = PINMUX_TYPE_NONE;
  758. cfg->name = "none";
  759. }
  760. return 0;
  761. }
  762. static int sh_pfc_pinctrl_probe(struct udevice *dev)
  763. {
  764. struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
  765. enum sh_pfc_model model = dev_get_driver_data(dev);
  766. fdt_addr_t base;
  767. base = dev_read_addr(dev);
  768. if (base == FDT_ADDR_T_NONE)
  769. return -EINVAL;
  770. priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
  771. if (!priv->pfc.regs)
  772. return -ENOMEM;
  773. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  774. if (model == SH_PFC_R8A7790)
  775. priv->pfc.info = &r8a7790_pinmux_info;
  776. #endif
  777. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  778. if (model == SH_PFC_R8A7791)
  779. priv->pfc.info = &r8a7791_pinmux_info;
  780. #endif
  781. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  782. if (model == SH_PFC_R8A7792)
  783. priv->pfc.info = &r8a7792_pinmux_info;
  784. #endif
  785. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  786. if (model == SH_PFC_R8A7793)
  787. priv->pfc.info = &r8a7793_pinmux_info;
  788. #endif
  789. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  790. if (model == SH_PFC_R8A7794)
  791. priv->pfc.info = &r8a7794_pinmux_info;
  792. #endif
  793. #ifdef CONFIG_PINCTRL_PFC_R8A77951
  794. if (model == SH_PFC_R8A7795)
  795. priv->pfc.info = &r8a77951_pinmux_info;
  796. #endif
  797. #ifdef CONFIG_PINCTRL_PFC_R8A77960
  798. if (model == SH_PFC_R8A77960)
  799. priv->pfc.info = &r8a77960_pinmux_info;
  800. #endif
  801. #ifdef CONFIG_PINCTRL_PFC_R8A77961
  802. if (model == SH_PFC_R8A77961)
  803. priv->pfc.info = &r8a77961_pinmux_info;
  804. #endif
  805. #ifdef CONFIG_PINCTRL_PFC_R8A774A1
  806. if (model == SH_PFC_R8A774A1)
  807. priv->pfc.info = &r8a774a1_pinmux_info;
  808. #endif
  809. #ifdef CONFIG_PINCTRL_PFC_R8A774B1
  810. if (model == SH_PFC_R8A774B1)
  811. priv->pfc.info = &r8a774b1_pinmux_info;
  812. #endif
  813. #ifdef CONFIG_PINCTRL_PFC_R8A774C0
  814. if (model == SH_PFC_R8A774C0)
  815. priv->pfc.info = &r8a774c0_pinmux_info;
  816. #endif
  817. #ifdef CONFIG_PINCTRL_PFC_R8A774E1
  818. if (model == SH_PFC_R8A774E1)
  819. priv->pfc.info = &r8a774e1_pinmux_info;
  820. #endif
  821. #ifdef CONFIG_PINCTRL_PFC_R8A77965
  822. if (model == SH_PFC_R8A77965)
  823. priv->pfc.info = &r8a77965_pinmux_info;
  824. #endif
  825. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  826. if (model == SH_PFC_R8A77970)
  827. priv->pfc.info = &r8a77970_pinmux_info;
  828. #endif
  829. #ifdef CONFIG_PINCTRL_PFC_R8A77980
  830. if (model == SH_PFC_R8A77980)
  831. priv->pfc.info = &r8a77980_pinmux_info;
  832. #endif
  833. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  834. if (model == SH_PFC_R8A77990)
  835. priv->pfc.info = &r8a77990_pinmux_info;
  836. #endif
  837. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  838. if (model == SH_PFC_R8A77995)
  839. priv->pfc.info = &r8a77995_pinmux_info;
  840. #endif
  841. #ifdef CONFIG_PINCTRL_PFC_R8A779A0
  842. if (model == SH_PFC_R8A779A0)
  843. priv->pfc.info = &r8a779a0_pinmux_info;
  844. #endif
  845. #ifdef CONFIG_PINCTRL_PFC_R8A779F0
  846. if (model == SH_PFC_R8A779F0)
  847. priv->pfc.info = &r8a779f0_pinmux_info;
  848. #endif
  849. #ifdef CONFIG_PINCTRL_PFC_R8A779G0
  850. if (model == SH_PFC_R8A779G0)
  851. priv->pfc.info = &r8a779g0_pinmux_info;
  852. #endif
  853. priv->pmx.pfc = &priv->pfc;
  854. sh_pfc_init_ranges(&priv->pfc);
  855. sh_pfc_map_pins(&priv->pfc, &priv->pmx);
  856. return 0;
  857. }
  858. static const struct udevice_id sh_pfc_pinctrl_ids[] = {
  859. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  860. {
  861. .compatible = "renesas,pfc-r8a7790",
  862. .data = SH_PFC_R8A7790,
  863. },
  864. #endif
  865. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  866. {
  867. .compatible = "renesas,pfc-r8a7791",
  868. .data = SH_PFC_R8A7791,
  869. },
  870. #endif
  871. #ifdef CONFIG_PINCTRL_PFC_R8A7792
  872. {
  873. .compatible = "renesas,pfc-r8a7792",
  874. .data = SH_PFC_R8A7792,
  875. },
  876. #endif
  877. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  878. {
  879. .compatible = "renesas,pfc-r8a7793",
  880. .data = SH_PFC_R8A7793,
  881. },
  882. #endif
  883. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  884. {
  885. .compatible = "renesas,pfc-r8a7794",
  886. .data = SH_PFC_R8A7794,
  887. },
  888. #endif
  889. #ifdef CONFIG_PINCTRL_PFC_R8A77951
  890. {
  891. .compatible = "renesas,pfc-r8a7795",
  892. .data = SH_PFC_R8A7795,
  893. },
  894. #endif
  895. #ifdef CONFIG_PINCTRL_PFC_R8A77960
  896. {
  897. .compatible = "renesas,pfc-r8a7796",
  898. .data = SH_PFC_R8A77960,
  899. },
  900. #endif
  901. #ifdef CONFIG_PINCTRL_PFC_R8A77961
  902. {
  903. .compatible = "renesas,pfc-r8a77961",
  904. .data = SH_PFC_R8A77961,
  905. },
  906. #endif
  907. #ifdef CONFIG_PINCTRL_PFC_R8A774A1
  908. {
  909. .compatible = "renesas,pfc-r8a774a1",
  910. .data = SH_PFC_R8A774A1,
  911. },
  912. #endif
  913. #ifdef CONFIG_PINCTRL_PFC_R8A774B1
  914. {
  915. .compatible = "renesas,pfc-r8a774b1",
  916. .data = SH_PFC_R8A774B1,
  917. },
  918. #endif
  919. #ifdef CONFIG_PINCTRL_PFC_R8A774C0
  920. {
  921. .compatible = "renesas,pfc-r8a774c0",
  922. .data = SH_PFC_R8A774C0,
  923. },
  924. #endif
  925. #ifdef CONFIG_PINCTRL_PFC_R8A774E1
  926. {
  927. .compatible = "renesas,pfc-r8a774e1",
  928. .data = SH_PFC_R8A774E1,
  929. },
  930. #endif
  931. #ifdef CONFIG_PINCTRL_PFC_R8A77965
  932. {
  933. .compatible = "renesas,pfc-r8a77965",
  934. .data = SH_PFC_R8A77965,
  935. },
  936. #endif
  937. #ifdef CONFIG_PINCTRL_PFC_R8A77970
  938. {
  939. .compatible = "renesas,pfc-r8a77970",
  940. .data = SH_PFC_R8A77970,
  941. },
  942. #endif
  943. #ifdef CONFIG_PINCTRL_PFC_R8A77980
  944. {
  945. .compatible = "renesas,pfc-r8a77980",
  946. .data = SH_PFC_R8A77980,
  947. },
  948. #endif
  949. #ifdef CONFIG_PINCTRL_PFC_R8A77990
  950. {
  951. .compatible = "renesas,pfc-r8a77990",
  952. .data = SH_PFC_R8A77990,
  953. },
  954. #endif
  955. #ifdef CONFIG_PINCTRL_PFC_R8A77995
  956. {
  957. .compatible = "renesas,pfc-r8a77995",
  958. .data = SH_PFC_R8A77995,
  959. },
  960. #endif
  961. #ifdef CONFIG_PINCTRL_PFC_R8A779A0
  962. {
  963. .compatible = "renesas,pfc-r8a779a0",
  964. .data = SH_PFC_R8A779A0,
  965. },
  966. #endif
  967. #ifdef CONFIG_PINCTRL_PFC_R8A779F0
  968. {
  969. .compatible = "renesas,pfc-r8a779f0",
  970. .data = SH_PFC_R8A779F0,
  971. },
  972. #endif
  973. #ifdef CONFIG_PINCTRL_PFC_R8A779G0
  974. {
  975. .compatible = "renesas,pfc-r8a779g0",
  976. .data = SH_PFC_R8A779G0,
  977. },
  978. #endif
  979. { },
  980. };
  981. U_BOOT_DRIVER(pinctrl_sh_pfc) = {
  982. .name = "sh_pfc_pinctrl",
  983. .id = UCLASS_PINCTRL,
  984. .of_match = sh_pfc_pinctrl_ids,
  985. .priv_auto = sizeof(struct sh_pfc_pinctrl_priv),
  986. .ops = &sh_pfc_pinctrl_ops,
  987. .probe = sh_pfc_pinctrl_probe,
  988. };