pinctrl-single.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
  4. * Copyright (C) 2021 Dario Binacchi <dariobin@libero.it>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <dm/device_compat.h>
  9. #include <dm/devres.h>
  10. #include <dm/of_access.h>
  11. #include <dm/pinctrl.h>
  12. #include <linux/libfdt.h>
  13. #include <linux/list.h>
  14. #include <asm/io.h>
  15. #include <sort.h>
  16. /**
  17. * struct single_pdata - platform data
  18. * @base: first configuration register
  19. * @offset: index of last configuration register
  20. * @mask: configuration-value mask bits
  21. * @width: configuration register bit width
  22. * @bits_per_mux: true if one register controls more than one pin
  23. */
  24. struct single_pdata {
  25. fdt_addr_t base;
  26. int offset;
  27. u32 mask;
  28. u32 width;
  29. u32 args_count;
  30. bool bits_per_mux;
  31. };
  32. /**
  33. * struct single_func - pinctrl function
  34. * @node: list node
  35. * @name: pinctrl function name
  36. * @npins: number of entries in pins array
  37. * @pins: pins array
  38. */
  39. struct single_func {
  40. struct list_head node;
  41. const char *name;
  42. unsigned int npins;
  43. unsigned int *pins;
  44. };
  45. /**
  46. * struct single_gpiofunc_range - pin ranges with same mux value of gpio fun
  47. * @offset: offset base of pins
  48. * @npins: number pins with the same mux value of gpio function
  49. * @gpiofunc: mux value of gpio function
  50. * @node: list node
  51. */
  52. struct single_gpiofunc_range {
  53. u32 offset;
  54. u32 npins;
  55. u32 gpiofunc;
  56. struct list_head node;
  57. };
  58. /**
  59. * struct single_priv - private data
  60. * @bits_per_pin: number of bits per pin
  61. * @npins: number of selectable pins
  62. * @pin_name: temporary buffer to store the pin name
  63. * @functions: list pin functions
  64. * @gpiofuncs: list gpio functions
  65. */
  66. struct single_priv {
  67. #if (IS_ENABLED(CONFIG_SANDBOX))
  68. u32 *sandbox_regs;
  69. #endif
  70. unsigned int bits_per_pin;
  71. unsigned int npins;
  72. char pin_name[PINNAME_SIZE];
  73. struct list_head functions;
  74. struct list_head gpiofuncs;
  75. };
  76. /**
  77. * struct single_fdt_bits_cfg - pin configuration
  78. *
  79. * This structure is used for the pin configuration parameters in case
  80. * the register controls more than one pin.
  81. *
  82. * @reg: configuration register offset
  83. * @val: configuration register value
  84. * @mask: configuration register mask
  85. */
  86. struct single_fdt_bits_cfg {
  87. fdt32_t reg;
  88. fdt32_t val;
  89. fdt32_t mask;
  90. };
  91. #if (!IS_ENABLED(CONFIG_SANDBOX))
  92. static unsigned int single_read(struct udevice *dev, fdt_addr_t reg)
  93. {
  94. struct single_pdata *pdata = dev_get_plat(dev);
  95. switch (pdata->width) {
  96. case 8:
  97. return readb(reg);
  98. case 16:
  99. return readw(reg);
  100. default: /* 32 bits */
  101. return readl(reg);
  102. }
  103. return readb(reg);
  104. }
  105. static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg)
  106. {
  107. struct single_pdata *pdata = dev_get_plat(dev);
  108. switch (pdata->width) {
  109. case 8:
  110. writeb(val, reg);
  111. break;
  112. case 16:
  113. writew(val, reg);
  114. break;
  115. default: /* 32 bits */
  116. writel(val, reg);
  117. }
  118. }
  119. #else /* CONFIG_SANDBOX */
  120. static unsigned int single_read(struct udevice *dev, fdt_addr_t reg)
  121. {
  122. struct single_priv *priv = dev_get_priv(dev);
  123. return priv->sandbox_regs[reg];
  124. }
  125. static void single_write(struct udevice *dev, unsigned int val, fdt_addr_t reg)
  126. {
  127. struct single_priv *priv = dev_get_priv(dev);
  128. priv->sandbox_regs[reg] = val;
  129. }
  130. #endif /* CONFIG_SANDBOX */
  131. /**
  132. * single_get_pin_by_offset() - get a pin based on the register offset
  133. * @dev: single driver instance
  134. * @offset: register offset from the base
  135. */
  136. static int single_get_pin_by_offset(struct udevice *dev, unsigned int offset)
  137. {
  138. struct single_pdata *pdata = dev_get_plat(dev);
  139. struct single_priv *priv = dev_get_priv(dev);
  140. if (offset > pdata->offset) {
  141. dev_err(dev, "mux offset out of range: 0x%x (0x%x)\n",
  142. offset, pdata->offset);
  143. return -EINVAL;
  144. }
  145. if (pdata->bits_per_mux)
  146. return (offset * BITS_PER_BYTE) / priv->bits_per_pin;
  147. return offset / (pdata->width / BITS_PER_BYTE);
  148. }
  149. static int single_get_offset_by_pin(struct udevice *dev, unsigned int pin)
  150. {
  151. struct single_pdata *pdata = dev_get_plat(dev);
  152. struct single_priv *priv = dev_get_priv(dev);
  153. unsigned int mux_bytes;
  154. if (pin >= priv->npins)
  155. return -EINVAL;
  156. mux_bytes = pdata->width / BITS_PER_BYTE;
  157. if (pdata->bits_per_mux) {
  158. int byte_num;
  159. byte_num = (priv->bits_per_pin * pin) / BITS_PER_BYTE;
  160. return (byte_num / mux_bytes) * mux_bytes;
  161. }
  162. return pin * mux_bytes;
  163. }
  164. static const char *single_get_pin_function(struct udevice *dev,
  165. unsigned int pin)
  166. {
  167. struct single_priv *priv = dev_get_priv(dev);
  168. struct single_func *func;
  169. int i;
  170. list_for_each_entry(func, &priv->functions, node) {
  171. for (i = 0; i < func->npins; i++) {
  172. if (pin == func->pins[i])
  173. return func->name;
  174. if (pin < func->pins[i])
  175. break;
  176. }
  177. }
  178. return NULL;
  179. }
  180. static int single_get_pin_muxing(struct udevice *dev, unsigned int pin,
  181. char *buf, int size)
  182. {
  183. struct single_pdata *pdata = dev_get_plat(dev);
  184. struct single_priv *priv = dev_get_priv(dev);
  185. fdt_addr_t reg;
  186. const char *fname;
  187. unsigned int val;
  188. int offset, pin_shift = 0;
  189. offset = single_get_offset_by_pin(dev, pin);
  190. if (offset < 0)
  191. return offset;
  192. reg = pdata->base + offset;
  193. val = single_read(dev, reg);
  194. if (pdata->bits_per_mux)
  195. pin_shift = pin % (pdata->width / priv->bits_per_pin) *
  196. priv->bits_per_pin;
  197. val &= (pdata->mask << pin_shift);
  198. fname = single_get_pin_function(dev, pin);
  199. snprintf(buf, size, "%pa 0x%08x %s", &reg, val,
  200. fname ? fname : "UNCLAIMED");
  201. return 0;
  202. }
  203. static int single_request(struct udevice *dev, int pin, int flags)
  204. {
  205. struct single_priv *priv = dev_get_priv(dev);
  206. struct single_pdata *pdata = dev_get_plat(dev);
  207. struct single_gpiofunc_range *frange = NULL;
  208. struct list_head *pos, *tmp;
  209. phys_addr_t reg;
  210. int mux_bytes = 0;
  211. u32 data;
  212. /* If function mask is null, needn't enable it. */
  213. if (!pdata->mask)
  214. return -ENOTSUPP;
  215. list_for_each_safe(pos, tmp, &priv->gpiofuncs) {
  216. frange = list_entry(pos, struct single_gpiofunc_range, node);
  217. if ((pin >= frange->offset + frange->npins) ||
  218. pin < frange->offset)
  219. continue;
  220. mux_bytes = pdata->width / BITS_PER_BYTE;
  221. reg = pdata->base + pin * mux_bytes;
  222. data = single_read(dev, reg);
  223. data &= ~pdata->mask;
  224. data |= frange->gpiofunc;
  225. single_write(dev, data, reg);
  226. break;
  227. }
  228. return 0;
  229. }
  230. static struct single_func *single_allocate_function(struct udevice *dev,
  231. unsigned int group_pins)
  232. {
  233. struct single_func *func;
  234. func = devm_kmalloc(dev, sizeof(*func), GFP_KERNEL);
  235. if (!func)
  236. return ERR_PTR(-ENOMEM);
  237. func->pins = devm_kmalloc(dev, sizeof(unsigned int) * group_pins,
  238. GFP_KERNEL);
  239. if (!func->pins)
  240. return ERR_PTR(-ENOMEM);
  241. return func;
  242. }
  243. static int single_pin_compare(const void *s1, const void *s2)
  244. {
  245. int pin1 = *(const unsigned int *)s1;
  246. int pin2 = *(const unsigned int *)s2;
  247. return pin1 - pin2;
  248. }
  249. /**
  250. * single_configure_pins() - Configure pins based on FDT data
  251. *
  252. * @dev: Pointer to single pin configuration device which is the parent of
  253. * the pins node holding the pin configuration data.
  254. * @pins: Pointer to the first element of an array of register/value pairs
  255. * of type 'u32'. Each such pair describes the pin to be configured
  256. * and the value to be used for configuration.
  257. * The value can either be a simple value if #pinctrl-cells = 1
  258. * or a configuration value and a pin mux mode value if it is 2
  259. * This pointer points to a 'pinctrl-single,pins' property in the
  260. * device-tree.
  261. * @size: Size of the 'pins' array in bytes.
  262. * The number of cells in the array therefore equals to
  263. * 'size / sizeof(u32)'.
  264. * @fname: Function name.
  265. */
  266. static int single_configure_pins(struct udevice *dev,
  267. const u32 *pins,
  268. int size, const char *fname)
  269. {
  270. struct single_pdata *pdata = dev_get_plat(dev);
  271. struct single_priv *priv = dev_get_priv(dev);
  272. int stride = pdata->args_count + 1;
  273. int n, pin, count = size / sizeof(u32);
  274. struct single_func *func;
  275. phys_addr_t reg;
  276. u32 offset, val, mux;
  277. /* If function mask is null, needn't enable it. */
  278. if (!pdata->mask)
  279. return 0;
  280. func = single_allocate_function(dev, count);
  281. if (IS_ERR(func))
  282. return PTR_ERR(func);
  283. func->name = fname;
  284. func->npins = 0;
  285. for (n = 0; n < count; n += stride) {
  286. offset = fdt32_to_cpu(pins[n]);
  287. if (offset > pdata->offset) {
  288. dev_err(dev, " invalid register offset 0x%x\n",
  289. offset);
  290. continue;
  291. }
  292. /* if the pinctrl-cells is 2 then the second cell contains the mux */
  293. if (stride == 3)
  294. mux = fdt32_to_cpu(pins[n + 2]);
  295. else
  296. mux = 0;
  297. reg = pdata->base + offset;
  298. val = (fdt32_to_cpu(pins[n + 1]) | mux) & pdata->mask;
  299. pin = single_get_pin_by_offset(dev, offset);
  300. if (pin < 0) {
  301. dev_err(dev, " failed to get pin by offset %x\n",
  302. offset);
  303. continue;
  304. }
  305. single_write(dev, (single_read(dev, reg) & ~pdata->mask) | val,
  306. reg);
  307. dev_dbg(dev, " reg/val %pa/0x%08x\n", &reg, val);
  308. func->pins[func->npins] = pin;
  309. func->npins++;
  310. }
  311. qsort(func->pins, func->npins, sizeof(func->pins[0]),
  312. single_pin_compare);
  313. list_add(&func->node, &priv->functions);
  314. return 0;
  315. }
  316. static int single_configure_bits(struct udevice *dev,
  317. const struct single_fdt_bits_cfg *pins,
  318. int size, const char *fname)
  319. {
  320. struct single_pdata *pdata = dev_get_plat(dev);
  321. struct single_priv *priv = dev_get_priv(dev);
  322. int n, pin, count = size / sizeof(struct single_fdt_bits_cfg);
  323. int npins_in_reg, pin_num_from_lsb;
  324. struct single_func *func;
  325. phys_addr_t reg;
  326. u32 offset, val, mask, bit_pos, val_pos, mask_pos, submask;
  327. /* If function mask is null, needn't enable it. */
  328. if (!pdata->mask)
  329. return 0;
  330. npins_in_reg = pdata->width / priv->bits_per_pin;
  331. func = single_allocate_function(dev, count * npins_in_reg);
  332. if (IS_ERR(func))
  333. return PTR_ERR(func);
  334. func->name = fname;
  335. func->npins = 0;
  336. for (n = 0; n < count; n++, pins++) {
  337. offset = fdt32_to_cpu(pins->reg);
  338. if (offset > pdata->offset) {
  339. dev_dbg(dev, " invalid register offset 0x%x\n",
  340. offset);
  341. continue;
  342. }
  343. reg = pdata->base + offset;
  344. pin = single_get_pin_by_offset(dev, offset);
  345. if (pin < 0) {
  346. dev_err(dev, " failed to get pin by offset 0x%pa\n",
  347. &reg);
  348. continue;
  349. }
  350. mask = fdt32_to_cpu(pins->mask);
  351. val = fdt32_to_cpu(pins->val) & mask;
  352. single_write(dev, (single_read(dev, reg) & ~mask) | val, reg);
  353. dev_dbg(dev, " reg/val %pa/0x%08x\n", &reg, val);
  354. while (mask) {
  355. bit_pos = __ffs(mask);
  356. pin_num_from_lsb = bit_pos / priv->bits_per_pin;
  357. mask_pos = pdata->mask << bit_pos;
  358. val_pos = val & mask_pos;
  359. submask = mask & mask_pos;
  360. if ((mask & mask_pos) == 0) {
  361. dev_err(dev, "Invalid mask at 0x%x\n", offset);
  362. break;
  363. }
  364. mask &= ~mask_pos;
  365. if (submask != mask_pos) {
  366. dev_warn(dev,
  367. "Invalid submask 0x%x at 0x%x\n",
  368. submask, offset);
  369. continue;
  370. }
  371. func->pins[func->npins] = pin + pin_num_from_lsb;
  372. func->npins++;
  373. }
  374. }
  375. qsort(func->pins, func->npins, sizeof(func->pins[0]),
  376. single_pin_compare);
  377. list_add(&func->node, &priv->functions);
  378. return 0;
  379. }
  380. static int single_set_state(struct udevice *dev,
  381. struct udevice *config)
  382. {
  383. const u32 *prop;
  384. const struct single_fdt_bits_cfg *prop_bits;
  385. int len;
  386. prop = dev_read_prop(config, "pinctrl-single,pins", &len);
  387. if (prop) {
  388. dev_dbg(dev, "configuring pins for %s\n", config->name);
  389. if (len % sizeof(u32)) {
  390. dev_dbg(dev, " invalid pin configuration in fdt\n");
  391. return -FDT_ERR_BADSTRUCTURE;
  392. }
  393. single_configure_pins(dev, prop, len, config->name);
  394. return 0;
  395. }
  396. /* pinctrl-single,pins not found so check for pinctrl-single,bits */
  397. prop_bits = dev_read_prop(config, "pinctrl-single,bits", &len);
  398. if (prop_bits) {
  399. dev_dbg(dev, "configuring pins for %s\n", config->name);
  400. if (len % sizeof(struct single_fdt_bits_cfg)) {
  401. dev_dbg(dev, " invalid bits configuration in fdt\n");
  402. return -FDT_ERR_BADSTRUCTURE;
  403. }
  404. single_configure_bits(dev, prop_bits, len, config->name);
  405. return 0;
  406. }
  407. /* Neither 'pinctrl-single,pins' nor 'pinctrl-single,bits' were found */
  408. return len;
  409. }
  410. static const char *single_get_pin_name(struct udevice *dev,
  411. unsigned int selector)
  412. {
  413. struct single_priv *priv = dev_get_priv(dev);
  414. if (selector >= priv->npins)
  415. snprintf(priv->pin_name, PINNAME_SIZE, "Error");
  416. else
  417. snprintf(priv->pin_name, PINNAME_SIZE, "PIN%u", selector);
  418. return priv->pin_name;
  419. }
  420. static int single_get_pins_count(struct udevice *dev)
  421. {
  422. struct single_priv *priv = dev_get_priv(dev);
  423. return priv->npins;
  424. }
  425. static int single_add_gpio_func(struct udevice *dev)
  426. {
  427. struct single_priv *priv = dev_get_priv(dev);
  428. const char *propname = "pinctrl-single,gpio-range";
  429. const char *cellname = "#pinctrl-single,gpio-range-cells";
  430. struct single_gpiofunc_range *range;
  431. struct ofnode_phandle_args gpiospec;
  432. int ret, i;
  433. for (i = 0; ; i++) {
  434. ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), propname,
  435. cellname, 0, i, &gpiospec);
  436. /* Do not treat it as error. Only treat it as end condition. */
  437. if (ret) {
  438. ret = 0;
  439. break;
  440. }
  441. range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
  442. if (!range) {
  443. ret = -ENOMEM;
  444. break;
  445. }
  446. range->offset = gpiospec.args[0];
  447. range->npins = gpiospec.args[1];
  448. range->gpiofunc = gpiospec.args[2];
  449. list_add_tail(&range->node, &priv->gpiofuncs);
  450. }
  451. return ret;
  452. }
  453. static int single_probe(struct udevice *dev)
  454. {
  455. struct single_pdata *pdata = dev_get_plat(dev);
  456. struct single_priv *priv = dev_get_priv(dev);
  457. u32 size;
  458. INIT_LIST_HEAD(&priv->functions);
  459. INIT_LIST_HEAD(&priv->gpiofuncs);
  460. size = pdata->offset + pdata->width / BITS_PER_BYTE;
  461. #if (IS_ENABLED(CONFIG_SANDBOX))
  462. priv->sandbox_regs =
  463. devm_kzalloc(dev, size * sizeof(*priv->sandbox_regs),
  464. GFP_KERNEL);
  465. if (!priv->sandbox_regs)
  466. return -ENOMEM;
  467. #endif
  468. /* looks like a possible divide by 0, but data->width avoids this */
  469. priv->npins = size / (pdata->width / BITS_PER_BYTE);
  470. if (pdata->bits_per_mux) {
  471. if (!pdata->mask) {
  472. dev_err(dev, "function mask needs to be non-zero\n");
  473. return -EINVAL;
  474. }
  475. priv->bits_per_pin = fls(pdata->mask);
  476. priv->npins *= (pdata->width / priv->bits_per_pin);
  477. }
  478. if (single_add_gpio_func(dev))
  479. dev_dbg(dev, "gpio functions are not added\n");
  480. dev_dbg(dev, "%d pins\n", priv->npins);
  481. return 0;
  482. }
  483. static int single_of_to_plat(struct udevice *dev)
  484. {
  485. fdt_addr_t addr;
  486. fdt_size_t size;
  487. struct single_pdata *pdata = dev_get_plat(dev);
  488. int ret;
  489. ret = dev_read_u32(dev, "pinctrl-single,register-width", &pdata->width);
  490. if (ret) {
  491. dev_err(dev, "missing register width\n");
  492. return ret;
  493. }
  494. switch (pdata->width) {
  495. case 8:
  496. case 16:
  497. case 32:
  498. break;
  499. default:
  500. dev_err(dev, "wrong register width\n");
  501. return -EINVAL;
  502. }
  503. addr = dev_read_addr_size_index(dev, 0, &size);
  504. if (addr == FDT_ADDR_T_NONE) {
  505. dev_err(dev, "failed to get base register address\n");
  506. return -EINVAL;
  507. }
  508. pdata->offset = size - pdata->width / BITS_PER_BYTE;
  509. pdata->base = addr;
  510. ret = dev_read_u32(dev, "pinctrl-single,function-mask", &pdata->mask);
  511. if (ret) {
  512. pdata->mask = 0;
  513. dev_warn(dev, "missing function register mask\n");
  514. }
  515. pdata->bits_per_mux = dev_read_bool(dev, "pinctrl-single,bit-per-mux");
  516. /* If no pinctrl-cells is present, default to old style of 2 cells with
  517. * bits per mux and 1 cell otherwise.
  518. */
  519. ret = dev_read_u32(dev, "#pinctrl-cells", &pdata->args_count);
  520. if (ret)
  521. pdata->args_count = pdata->bits_per_mux ? 2 : 1;
  522. return 0;
  523. }
  524. const struct pinctrl_ops single_pinctrl_ops = {
  525. .get_pins_count = single_get_pins_count,
  526. .get_pin_name = single_get_pin_name,
  527. .set_state = single_set_state,
  528. .get_pin_muxing = single_get_pin_muxing,
  529. .request = single_request,
  530. };
  531. static const struct udevice_id single_pinctrl_match[] = {
  532. { .compatible = "pinctrl-single" },
  533. { /* sentinel */ }
  534. };
  535. U_BOOT_DRIVER(single_pinctrl) = {
  536. .name = "single-pinctrl",
  537. .id = UCLASS_PINCTRL,
  538. .of_match = single_pinctrl_match,
  539. .ops = &single_pinctrl_ops,
  540. .plat_auto = sizeof(struct single_pdata),
  541. .priv_auto = sizeof(struct single_priv),
  542. .of_to_plat = single_of_to_plat,
  543. .probe = single_probe,
  544. };