core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. if (pfc->info->ranges == NULL)
  69. return pin;
  70. for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
  71. const struct pinmux_range *range = &pfc->info->ranges[i];
  72. if (pin <= range->end)
  73. return pin >= range->begin
  74. ? offset + pin - range->begin : -1;
  75. offset += range->end - range->begin + 1;
  76. }
  77. return -EINVAL;
  78. }
  79. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  80. {
  81. if (enum_id < r->begin)
  82. return 0;
  83. if (enum_id > r->end)
  84. return 0;
  85. return 1;
  86. }
  87. unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  88. unsigned long reg_width)
  89. {
  90. switch (reg_width) {
  91. case 8:
  92. return ioread8(mapped_reg);
  93. case 16:
  94. return ioread16(mapped_reg);
  95. case 32:
  96. return ioread32(mapped_reg);
  97. }
  98. BUG();
  99. return 0;
  100. }
  101. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned long reg_width,
  102. unsigned long data)
  103. {
  104. switch (reg_width) {
  105. case 8:
  106. iowrite8(data, mapped_reg);
  107. return;
  108. case 16:
  109. iowrite16(data, mapped_reg);
  110. return;
  111. case 32:
  112. iowrite32(data, mapped_reg);
  113. return;
  114. }
  115. BUG();
  116. }
  117. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  118. const struct pinmux_cfg_reg *crp,
  119. unsigned long in_pos,
  120. void __iomem **mapped_regp,
  121. unsigned long *maskp,
  122. unsigned long *posp)
  123. {
  124. int k;
  125. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  126. if (crp->field_width) {
  127. *maskp = (1 << crp->field_width) - 1;
  128. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  129. } else {
  130. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  131. *posp = crp->reg_width;
  132. for (k = 0; k <= in_pos; k++)
  133. *posp -= crp->var_field_width[k];
  134. }
  135. }
  136. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  137. const struct pinmux_cfg_reg *crp,
  138. unsigned long field, unsigned long value)
  139. {
  140. void __iomem *mapped_reg;
  141. unsigned long mask, pos, data;
  142. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  143. dev_dbg(pfc->dev, "write_reg addr = %lx, value = %ld, field = %ld, "
  144. "r_width = %ld, f_width = %ld\n",
  145. crp->reg, value, field, crp->reg_width, crp->field_width);
  146. mask = ~(mask << pos);
  147. value = value << pos;
  148. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  149. data &= mask;
  150. data |= value;
  151. if (pfc->info->unlock_reg)
  152. sh_pfc_write_raw_reg(
  153. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  154. ~data);
  155. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  156. }
  157. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  158. const struct pinmux_cfg_reg **crp, int *fieldp,
  159. int *valuep)
  160. {
  161. const struct pinmux_cfg_reg *config_reg;
  162. unsigned long r_width, f_width, curr_width, ncomb;
  163. int k, m, n, pos, bit_pos;
  164. k = 0;
  165. while (1) {
  166. config_reg = pfc->info->cfg_regs + k;
  167. r_width = config_reg->reg_width;
  168. f_width = config_reg->field_width;
  169. if (!r_width)
  170. break;
  171. pos = 0;
  172. m = 0;
  173. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  174. if (f_width)
  175. curr_width = f_width;
  176. else
  177. curr_width = config_reg->var_field_width[m];
  178. ncomb = 1 << curr_width;
  179. for (n = 0; n < ncomb; n++) {
  180. if (config_reg->enum_ids[pos + n] == enum_id) {
  181. *crp = config_reg;
  182. *fieldp = m;
  183. *valuep = n;
  184. return 0;
  185. }
  186. }
  187. pos += ncomb;
  188. m++;
  189. }
  190. k++;
  191. }
  192. return -EINVAL;
  193. }
  194. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  195. u16 *enum_idp)
  196. {
  197. const u16 *data = pfc->info->gpio_data;
  198. int k;
  199. if (pos) {
  200. *enum_idp = data[pos + 1];
  201. return pos + 1;
  202. }
  203. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  204. if (data[k] == mark) {
  205. *enum_idp = data[k + 1];
  206. return k + 1;
  207. }
  208. }
  209. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  210. mark);
  211. return -EINVAL;
  212. }
  213. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  214. {
  215. const struct pinmux_cfg_reg *cr = NULL;
  216. u16 enum_id;
  217. const struct pinmux_range *range;
  218. int in_range, pos, field, value;
  219. int ret;
  220. switch (pinmux_type) {
  221. case PINMUX_TYPE_GPIO:
  222. case PINMUX_TYPE_FUNCTION:
  223. range = NULL;
  224. break;
  225. case PINMUX_TYPE_OUTPUT:
  226. range = &pfc->info->output;
  227. break;
  228. case PINMUX_TYPE_INPUT:
  229. range = &pfc->info->input;
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. pos = 0;
  235. enum_id = 0;
  236. field = 0;
  237. value = 0;
  238. /* Iterate over all the configuration fields we need to update. */
  239. while (1) {
  240. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  241. if (pos < 0)
  242. return pos;
  243. if (!enum_id)
  244. break;
  245. /* Check if the configuration field selects a function. If it
  246. * doesn't, skip the field if it's not applicable to the
  247. * requested pinmux type.
  248. */
  249. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  250. if (!in_range) {
  251. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  252. /* Functions are allowed to modify all
  253. * fields.
  254. */
  255. in_range = 1;
  256. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  257. /* Input/output types can only modify fields
  258. * that correspond to their respective ranges.
  259. */
  260. in_range = sh_pfc_enum_in_range(enum_id, range);
  261. /*
  262. * special case pass through for fixed
  263. * input-only or output-only pins without
  264. * function enum register association.
  265. */
  266. if (in_range && enum_id == range->force)
  267. continue;
  268. }
  269. /* GPIOs are only allowed to modify function fields. */
  270. }
  271. if (!in_range)
  272. continue;
  273. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  274. if (ret < 0)
  275. return ret;
  276. sh_pfc_write_config_reg(pfc, cr, field, value);
  277. }
  278. return 0;
  279. }
  280. #ifdef CONFIG_OF
  281. static const struct of_device_id sh_pfc_of_table[] = {
  282. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  283. {
  284. .compatible = "renesas,pfc-r8a73a4",
  285. .data = &r8a73a4_pinmux_info,
  286. },
  287. #endif
  288. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  289. {
  290. .compatible = "renesas,pfc-r8a7740",
  291. .data = &r8a7740_pinmux_info,
  292. },
  293. #endif
  294. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  295. {
  296. .compatible = "renesas,pfc-r8a7778",
  297. .data = &r8a7778_pinmux_info,
  298. },
  299. #endif
  300. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  301. {
  302. .compatible = "renesas,pfc-r8a7779",
  303. .data = &r8a7779_pinmux_info,
  304. },
  305. #endif
  306. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  307. {
  308. .compatible = "renesas,pfc-r8a7790",
  309. .data = &r8a7790_pinmux_info,
  310. },
  311. #endif
  312. #ifdef CONFIG_PINCTRL_PFC_SH7372
  313. {
  314. .compatible = "renesas,pfc-sh7372",
  315. .data = &sh7372_pinmux_info,
  316. },
  317. #endif
  318. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  319. {
  320. .compatible = "renesas,pfc-sh73a0",
  321. .data = &sh73a0_pinmux_info,
  322. },
  323. #endif
  324. { },
  325. };
  326. MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
  327. #endif
  328. static int sh_pfc_probe(struct platform_device *pdev)
  329. {
  330. const struct platform_device_id *platid = platform_get_device_id(pdev);
  331. #ifdef CONFIG_OF
  332. struct device_node *np = pdev->dev.of_node;
  333. #endif
  334. const struct sh_pfc_soc_info *info;
  335. struct sh_pfc *pfc;
  336. int ret;
  337. #ifdef CONFIG_OF
  338. if (np)
  339. info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
  340. else
  341. #endif
  342. info = platid ? (const void *)platid->driver_data : NULL;
  343. if (info == NULL)
  344. return -ENODEV;
  345. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  346. if (pfc == NULL)
  347. return -ENOMEM;
  348. pfc->info = info;
  349. pfc->dev = &pdev->dev;
  350. ret = sh_pfc_ioremap(pfc, pdev);
  351. if (unlikely(ret < 0))
  352. return ret;
  353. spin_lock_init(&pfc->lock);
  354. if (info->ops && info->ops->init) {
  355. ret = info->ops->init(pfc);
  356. if (ret < 0)
  357. return ret;
  358. }
  359. pinctrl_provide_dummies();
  360. /*
  361. * Initialize pinctrl bindings first
  362. */
  363. ret = sh_pfc_register_pinctrl(pfc);
  364. if (unlikely(ret != 0))
  365. goto error;
  366. #ifdef CONFIG_GPIO_SH_PFC
  367. /*
  368. * Then the GPIO chip
  369. */
  370. ret = sh_pfc_register_gpiochip(pfc);
  371. if (unlikely(ret != 0)) {
  372. /*
  373. * If the GPIO chip fails to come up we still leave the
  374. * PFC state as it is, given that there are already
  375. * extant users of it that have succeeded by this point.
  376. */
  377. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  378. }
  379. #endif
  380. platform_set_drvdata(pdev, pfc);
  381. dev_info(pfc->dev, "%s support registered\n", info->name);
  382. return 0;
  383. error:
  384. if (info->ops && info->ops->exit)
  385. info->ops->exit(pfc);
  386. return ret;
  387. }
  388. static int sh_pfc_remove(struct platform_device *pdev)
  389. {
  390. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  391. #ifdef CONFIG_GPIO_SH_PFC
  392. sh_pfc_unregister_gpiochip(pfc);
  393. #endif
  394. sh_pfc_unregister_pinctrl(pfc);
  395. if (pfc->info->ops && pfc->info->ops->exit)
  396. pfc->info->ops->exit(pfc);
  397. platform_set_drvdata(pdev, NULL);
  398. return 0;
  399. }
  400. static const struct platform_device_id sh_pfc_id_table[] = {
  401. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  402. { "pfc-r8a73a4", (kernel_ulong_t)&r8a73a4_pinmux_info },
  403. #endif
  404. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  405. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  406. #endif
  407. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  408. { "pfc-r8a7778", (kernel_ulong_t)&r8a7778_pinmux_info },
  409. #endif
  410. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  411. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  412. #endif
  413. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  414. { "pfc-r8a7790", (kernel_ulong_t)&r8a7790_pinmux_info },
  415. #endif
  416. #ifdef CONFIG_PINCTRL_PFC_SH7203
  417. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  418. #endif
  419. #ifdef CONFIG_PINCTRL_PFC_SH7264
  420. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  421. #endif
  422. #ifdef CONFIG_PINCTRL_PFC_SH7269
  423. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  424. #endif
  425. #ifdef CONFIG_PINCTRL_PFC_SH7372
  426. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  427. #endif
  428. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  429. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  430. #endif
  431. #ifdef CONFIG_PINCTRL_PFC_SH7720
  432. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  433. #endif
  434. #ifdef CONFIG_PINCTRL_PFC_SH7722
  435. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  436. #endif
  437. #ifdef CONFIG_PINCTRL_PFC_SH7723
  438. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  439. #endif
  440. #ifdef CONFIG_PINCTRL_PFC_SH7724
  441. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  442. #endif
  443. #ifdef CONFIG_PINCTRL_PFC_SH7734
  444. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  445. #endif
  446. #ifdef CONFIG_PINCTRL_PFC_SH7757
  447. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  448. #endif
  449. #ifdef CONFIG_PINCTRL_PFC_SH7785
  450. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  451. #endif
  452. #ifdef CONFIG_PINCTRL_PFC_SH7786
  453. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  454. #endif
  455. #ifdef CONFIG_PINCTRL_PFC_SHX3
  456. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  457. #endif
  458. { "sh-pfc", 0 },
  459. { },
  460. };
  461. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  462. static struct platform_driver sh_pfc_driver = {
  463. .probe = sh_pfc_probe,
  464. .remove = sh_pfc_remove,
  465. .id_table = sh_pfc_id_table,
  466. .driver = {
  467. .name = DRV_NAME,
  468. .owner = THIS_MODULE,
  469. .of_match_table = of_match_ptr(sh_pfc_of_table),
  470. },
  471. };
  472. static int __init sh_pfc_init(void)
  473. {
  474. return platform_driver_register(&sh_pfc_driver);
  475. }
  476. postcore_initcall(sh_pfc_init);
  477. static void __exit sh_pfc_exit(void)
  478. {
  479. platform_driver_unregister(&sh_pfc_driver);
  480. }
  481. module_exit(sh_pfc_exit);
  482. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  483. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  484. MODULE_LICENSE("GPL v2");