core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * SuperH Pin Function Controller support.
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. * Copyright (C) 2009 - 2012 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #define DRV_NAME "sh-pfc"
  12. #include <linux/bitops.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pinctrl/machine.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include "core.h"
  25. static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
  26. {
  27. struct resource *res;
  28. int k;
  29. if (pdev->num_resources == 0)
  30. return -EINVAL;
  31. pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
  32. sizeof(*pfc->window), GFP_NOWAIT);
  33. if (!pfc->window)
  34. return -ENOMEM;
  35. pfc->num_windows = pdev->num_resources;
  36. for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
  37. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  38. pfc->window[k].phys = res->start;
  39. pfc->window[k].size = resource_size(res);
  40. pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
  41. resource_size(res));
  42. if (!pfc->window[k].virt)
  43. return -ENOMEM;
  44. }
  45. return 0;
  46. }
  47. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  48. unsigned long address)
  49. {
  50. struct sh_pfc_window *window;
  51. unsigned int i;
  52. /* scan through physical windows and convert address */
  53. for (i = 0; i < pfc->num_windows; i++) {
  54. window = pfc->window + i;
  55. if (address < window->phys)
  56. continue;
  57. if (address >= (window->phys + window->size))
  58. continue;
  59. return window->virt + (address - window->phys);
  60. }
  61. BUG();
  62. return NULL;
  63. }
  64. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  65. {
  66. unsigned int offset;
  67. unsigned int i;
  68. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  69. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  70. if (pin <= range->end)
  71. return pin >= range->start
  72. ? offset + pin - range->start : -1;
  73. offset += range->end - range->start + 1;
  74. }
  75. return -EINVAL;
  76. }
  77. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  78. {
  79. if (enum_id < r->begin)
  80. return 0;
  81. if (enum_id > r->end)
  82. return 0;
  83. return 1;
  84. }
  85. unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  86. unsigned long reg_width)
  87. {
  88. switch (reg_width) {
  89. case 8:
  90. return ioread8(mapped_reg);
  91. case 16:
  92. return ioread16(mapped_reg);
  93. case 32:
  94. return ioread32(mapped_reg);
  95. }
  96. BUG();
  97. return 0;
  98. }
  99. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
  100. unsigned long data)
  101. {
  102. switch (reg_width) {
  103. case 8:
  104. iowrite8(data, mapped_reg);
  105. return;
  106. case 16:
  107. iowrite16(data, mapped_reg);
  108. return;
  109. case 32:
  110. iowrite32(data, mapped_reg);
  111. return;
  112. }
  113. BUG();
  114. }
  115. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  116. const struct pinmux_cfg_reg *crp,
  117. unsigned long in_pos,
  118. void __iomem **mapped_regp,
  119. unsigned long *maskp,
  120. unsigned long *posp)
  121. {
  122. int k;
  123. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  124. if (crp->field_width) {
  125. *maskp = (1 << crp->field_width) - 1;
  126. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  127. } else {
  128. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  129. *posp = crp->reg_width;
  130. for (k = 0; k <= in_pos; k++)
  131. *posp -= crp->var_field_width[k];
  132. }
  133. }
  134. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  135. const struct pinmux_cfg_reg *crp,
  136. unsigned long field, unsigned long value)
  137. {
  138. void __iomem *mapped_reg;
  139. unsigned long mask, pos, data;
  140. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  141. dev_dbg(pfc->dev, "write_reg addr = %lx, value = %ld, field = %ld, "
  142. "r_width = %ld, f_width = %ld\n",
  143. crp->reg, value, field, crp->reg_width, crp->field_width);
  144. mask = ~(mask << pos);
  145. value = value << pos;
  146. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  147. data &= mask;
  148. data |= value;
  149. if (pfc->info->unlock_reg)
  150. sh_pfc_write_raw_reg(
  151. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  152. ~data);
  153. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  154. }
  155. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  156. const struct pinmux_cfg_reg **crp, int *fieldp,
  157. int *valuep)
  158. {
  159. const struct pinmux_cfg_reg *config_reg;
  160. unsigned long r_width, f_width, curr_width, ncomb;
  161. int k, m, n, pos, bit_pos;
  162. k = 0;
  163. while (1) {
  164. config_reg = pfc->info->cfg_regs + k;
  165. r_width = config_reg->reg_width;
  166. f_width = config_reg->field_width;
  167. if (!r_width)
  168. break;
  169. pos = 0;
  170. m = 0;
  171. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  172. if (f_width)
  173. curr_width = f_width;
  174. else
  175. curr_width = config_reg->var_field_width[m];
  176. ncomb = 1 << curr_width;
  177. for (n = 0; n < ncomb; n++) {
  178. if (config_reg->enum_ids[pos + n] == enum_id) {
  179. *crp = config_reg;
  180. *fieldp = m;
  181. *valuep = n;
  182. return 0;
  183. }
  184. }
  185. pos += ncomb;
  186. m++;
  187. }
  188. k++;
  189. }
  190. return -EINVAL;
  191. }
  192. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  193. u16 *enum_idp)
  194. {
  195. const u16 *data = pfc->info->gpio_data;
  196. int k;
  197. if (pos) {
  198. *enum_idp = data[pos + 1];
  199. return pos + 1;
  200. }
  201. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  202. if (data[k] == mark) {
  203. *enum_idp = data[k + 1];
  204. return k + 1;
  205. }
  206. }
  207. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  208. mark);
  209. return -EINVAL;
  210. }
  211. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  212. {
  213. const struct pinmux_cfg_reg *cr = NULL;
  214. u16 enum_id;
  215. const struct pinmux_range *range;
  216. int in_range, pos, field, value;
  217. int ret;
  218. switch (pinmux_type) {
  219. case PINMUX_TYPE_GPIO:
  220. case PINMUX_TYPE_FUNCTION:
  221. range = NULL;
  222. break;
  223. case PINMUX_TYPE_OUTPUT:
  224. range = &pfc->info->output;
  225. break;
  226. case PINMUX_TYPE_INPUT:
  227. range = &pfc->info->input;
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. pos = 0;
  233. enum_id = 0;
  234. field = 0;
  235. value = 0;
  236. /* Iterate over all the configuration fields we need to update. */
  237. while (1) {
  238. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  239. if (pos < 0)
  240. return pos;
  241. if (!enum_id)
  242. break;
  243. /* Check if the configuration field selects a function. If it
  244. * doesn't, skip the field if it's not applicable to the
  245. * requested pinmux type.
  246. */
  247. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  248. if (!in_range) {
  249. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  250. /* Functions are allowed to modify all
  251. * fields.
  252. */
  253. in_range = 1;
  254. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  255. /* Input/output types can only modify fields
  256. * that correspond to their respective ranges.
  257. */
  258. in_range = sh_pfc_enum_in_range(enum_id, range);
  259. /*
  260. * special case pass through for fixed
  261. * input-only or output-only pins without
  262. * function enum register association.
  263. */
  264. if (in_range && enum_id == range->force)
  265. continue;
  266. }
  267. /* GPIOs are only allowed to modify function fields. */
  268. }
  269. if (!in_range)
  270. continue;
  271. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  272. if (ret < 0)
  273. return ret;
  274. sh_pfc_write_config_reg(pfc, cr, field, value);
  275. }
  276. return 0;
  277. }
  278. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  279. {
  280. struct sh_pfc_pin_range *range;
  281. unsigned int nr_ranges;
  282. unsigned int i;
  283. if (pfc->info->pins[0].pin == (u16)-1) {
  284. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  285. * in its pin arrays yet. Consider the pin numbers range as
  286. * continuous and allocate a single range.
  287. */
  288. pfc->nr_ranges = 1;
  289. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
  290. GFP_KERNEL);
  291. if (pfc->ranges == NULL)
  292. return -ENOMEM;
  293. pfc->ranges->start = 0;
  294. pfc->ranges->end = pfc->info->nr_pins - 1;
  295. pfc->nr_gpio_pins = pfc->info->nr_pins;
  296. return 0;
  297. }
  298. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  299. * be sorted by pin numbers, and pins without a GPIO port must come
  300. * last.
  301. */
  302. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  303. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  304. nr_ranges++;
  305. }
  306. pfc->nr_ranges = nr_ranges;
  307. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges) * nr_ranges,
  308. GFP_KERNEL);
  309. if (pfc->ranges == NULL)
  310. return -ENOMEM;
  311. range = pfc->ranges;
  312. range->start = pfc->info->pins[0].pin;
  313. for (i = 1; i < pfc->info->nr_pins; ++i) {
  314. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  315. continue;
  316. range->end = pfc->info->pins[i-1].pin;
  317. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  318. pfc->nr_gpio_pins = range->end + 1;
  319. range++;
  320. range->start = pfc->info->pins[i].pin;
  321. }
  322. range->end = pfc->info->pins[i-1].pin;
  323. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  324. pfc->nr_gpio_pins = range->end + 1;
  325. return 0;
  326. }
  327. #ifdef CONFIG_OF
  328. static const struct of_device_id sh_pfc_of_table[] = {
  329. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  330. {
  331. .compatible = "renesas,pfc-r8a73a4",
  332. .data = &r8a73a4_pinmux_info,
  333. },
  334. #endif
  335. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  336. {
  337. .compatible = "renesas,pfc-r8a7740",
  338. .data = &r8a7740_pinmux_info,
  339. },
  340. #endif
  341. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  342. {
  343. .compatible = "renesas,pfc-r8a7778",
  344. .data = &r8a7778_pinmux_info,
  345. },
  346. #endif
  347. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  348. {
  349. .compatible = "renesas,pfc-r8a7779",
  350. .data = &r8a7779_pinmux_info,
  351. },
  352. #endif
  353. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  354. {
  355. .compatible = "renesas,pfc-r8a7790",
  356. .data = &r8a7790_pinmux_info,
  357. },
  358. #endif
  359. #ifdef CONFIG_PINCTRL_PFC_SH7372
  360. {
  361. .compatible = "renesas,pfc-sh7372",
  362. .data = &sh7372_pinmux_info,
  363. },
  364. #endif
  365. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  366. {
  367. .compatible = "renesas,pfc-sh73a0",
  368. .data = &sh73a0_pinmux_info,
  369. },
  370. #endif
  371. { },
  372. };
  373. MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
  374. #endif
  375. static int sh_pfc_probe(struct platform_device *pdev)
  376. {
  377. const struct platform_device_id *platid = platform_get_device_id(pdev);
  378. #ifdef CONFIG_OF
  379. struct device_node *np = pdev->dev.of_node;
  380. #endif
  381. const struct sh_pfc_soc_info *info;
  382. struct sh_pfc *pfc;
  383. int ret;
  384. #ifdef CONFIG_OF
  385. if (np)
  386. info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
  387. else
  388. #endif
  389. info = platid ? (const void *)platid->driver_data : NULL;
  390. if (info == NULL)
  391. return -ENODEV;
  392. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  393. if (pfc == NULL)
  394. return -ENOMEM;
  395. pfc->info = info;
  396. pfc->dev = &pdev->dev;
  397. ret = sh_pfc_ioremap(pfc, pdev);
  398. if (unlikely(ret < 0))
  399. return ret;
  400. spin_lock_init(&pfc->lock);
  401. if (info->ops && info->ops->init) {
  402. ret = info->ops->init(pfc);
  403. if (ret < 0)
  404. return ret;
  405. }
  406. pinctrl_provide_dummies();
  407. ret = sh_pfc_init_ranges(pfc);
  408. if (ret < 0)
  409. return ret;
  410. /*
  411. * Initialize pinctrl bindings first
  412. */
  413. ret = sh_pfc_register_pinctrl(pfc);
  414. if (unlikely(ret != 0))
  415. goto error;
  416. #ifdef CONFIG_GPIO_SH_PFC
  417. /*
  418. * Then the GPIO chip
  419. */
  420. ret = sh_pfc_register_gpiochip(pfc);
  421. if (unlikely(ret != 0)) {
  422. /*
  423. * If the GPIO chip fails to come up we still leave the
  424. * PFC state as it is, given that there are already
  425. * extant users of it that have succeeded by this point.
  426. */
  427. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  428. }
  429. #endif
  430. platform_set_drvdata(pdev, pfc);
  431. dev_info(pfc->dev, "%s support registered\n", info->name);
  432. return 0;
  433. error:
  434. if (info->ops && info->ops->exit)
  435. info->ops->exit(pfc);
  436. return ret;
  437. }
  438. static int sh_pfc_remove(struct platform_device *pdev)
  439. {
  440. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  441. #ifdef CONFIG_GPIO_SH_PFC
  442. sh_pfc_unregister_gpiochip(pfc);
  443. #endif
  444. sh_pfc_unregister_pinctrl(pfc);
  445. if (pfc->info->ops && pfc->info->ops->exit)
  446. pfc->info->ops->exit(pfc);
  447. return 0;
  448. }
  449. static const struct platform_device_id sh_pfc_id_table[] = {
  450. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  451. { "pfc-r8a73a4", (kernel_ulong_t)&r8a73a4_pinmux_info },
  452. #endif
  453. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  454. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  455. #endif
  456. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  457. { "pfc-r8a7778", (kernel_ulong_t)&r8a7778_pinmux_info },
  458. #endif
  459. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  460. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  461. #endif
  462. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  463. { "pfc-r8a7790", (kernel_ulong_t)&r8a7790_pinmux_info },
  464. #endif
  465. #ifdef CONFIG_PINCTRL_PFC_SH7203
  466. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  467. #endif
  468. #ifdef CONFIG_PINCTRL_PFC_SH7264
  469. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  470. #endif
  471. #ifdef CONFIG_PINCTRL_PFC_SH7269
  472. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  473. #endif
  474. #ifdef CONFIG_PINCTRL_PFC_SH7372
  475. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  476. #endif
  477. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  478. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  479. #endif
  480. #ifdef CONFIG_PINCTRL_PFC_SH7720
  481. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  482. #endif
  483. #ifdef CONFIG_PINCTRL_PFC_SH7722
  484. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  485. #endif
  486. #ifdef CONFIG_PINCTRL_PFC_SH7723
  487. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  488. #endif
  489. #ifdef CONFIG_PINCTRL_PFC_SH7724
  490. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  491. #endif
  492. #ifdef CONFIG_PINCTRL_PFC_SH7734
  493. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  494. #endif
  495. #ifdef CONFIG_PINCTRL_PFC_SH7757
  496. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  497. #endif
  498. #ifdef CONFIG_PINCTRL_PFC_SH7785
  499. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  500. #endif
  501. #ifdef CONFIG_PINCTRL_PFC_SH7786
  502. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  503. #endif
  504. #ifdef CONFIG_PINCTRL_PFC_SHX3
  505. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  506. #endif
  507. { "sh-pfc", 0 },
  508. { },
  509. };
  510. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  511. static struct platform_driver sh_pfc_driver = {
  512. .probe = sh_pfc_probe,
  513. .remove = sh_pfc_remove,
  514. .id_table = sh_pfc_id_table,
  515. .driver = {
  516. .name = DRV_NAME,
  517. .owner = THIS_MODULE,
  518. .of_match_table = of_match_ptr(sh_pfc_of_table),
  519. },
  520. };
  521. static int __init sh_pfc_init(void)
  522. {
  523. return platform_driver_register(&sh_pfc_driver);
  524. }
  525. postcore_initcall(sh_pfc_init);
  526. static void __exit sh_pfc_exit(void)
  527. {
  528. platform_driver_unregister(&sh_pfc_driver);
  529. }
  530. module_exit(sh_pfc_exit);
  531. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  532. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  533. MODULE_LICENSE("GPL v2");