core.c 12 KB

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