core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/io.h>
  17. #include <linux/ioport.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include "core.h"
  24. static int sh_pfc_ioremap(struct sh_pfc *pfc, struct platform_device *pdev)
  25. {
  26. struct resource *res;
  27. int k;
  28. if (pdev->num_resources == 0)
  29. return -EINVAL;
  30. pfc->window = devm_kzalloc(pfc->dev, pdev->num_resources *
  31. sizeof(*pfc->window), GFP_NOWAIT);
  32. if (!pfc->window)
  33. return -ENOMEM;
  34. pfc->num_windows = pdev->num_resources;
  35. for (k = 0, res = pdev->resource; k < pdev->num_resources; k++, res++) {
  36. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  37. pfc->window[k].phys = res->start;
  38. pfc->window[k].size = resource_size(res);
  39. pfc->window[k].virt = devm_ioremap_nocache(pfc->dev, res->start,
  40. resource_size(res));
  41. if (!pfc->window[k].virt)
  42. return -ENOMEM;
  43. }
  44. return 0;
  45. }
  46. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  47. unsigned long address)
  48. {
  49. struct sh_pfc_window *window;
  50. unsigned int i;
  51. /* scan through physical windows and convert address */
  52. for (i = 0; i < pfc->num_windows; i++) {
  53. window = pfc->window + i;
  54. if (address < window->phys)
  55. continue;
  56. if (address >= (window->phys + window->size))
  57. continue;
  58. return window->virt + (address - window->phys);
  59. }
  60. BUG();
  61. }
  62. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  63. {
  64. unsigned int offset;
  65. unsigned int i;
  66. if (pfc->info->ranges == NULL)
  67. return pin;
  68. for (i = 0, offset = 0; i < pfc->info->nr_ranges; ++i) {
  69. const struct pinmux_range *range = &pfc->info->ranges[i];
  70. if (pin <= range->end)
  71. return pin >= range->begin
  72. ? offset + pin - range->begin : -1;
  73. offset += range->end - range->begin + 1;
  74. }
  75. return -1;
  76. }
  77. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id, 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. 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 int sh_pfc_read_config_reg(struct sh_pfc *pfc,
  135. struct pinmux_cfg_reg *crp,
  136. unsigned long field)
  137. {
  138. void __iomem *mapped_reg;
  139. unsigned long mask, pos;
  140. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  141. pr_debug("read_reg: addr = %lx, field = %ld, "
  142. "r_width = %ld, f_width = %ld\n",
  143. crp->reg, field, crp->reg_width, crp->field_width);
  144. return (sh_pfc_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  145. }
  146. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  147. struct pinmux_cfg_reg *crp,
  148. unsigned long field, unsigned long value)
  149. {
  150. void __iomem *mapped_reg;
  151. unsigned long mask, pos, data;
  152. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  153. pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
  154. "r_width = %ld, f_width = %ld\n",
  155. crp->reg, value, field, crp->reg_width, crp->field_width);
  156. mask = ~(mask << pos);
  157. value = value << pos;
  158. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  159. data &= mask;
  160. data |= value;
  161. if (pfc->info->unlock_reg)
  162. sh_pfc_write_raw_reg(
  163. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  164. ~data);
  165. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  166. }
  167. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  168. struct pinmux_cfg_reg **crp, int *fieldp,
  169. int *valuep, unsigned long **cntp)
  170. {
  171. struct pinmux_cfg_reg *config_reg;
  172. unsigned long r_width, f_width, curr_width, ncomb;
  173. int k, m, n, pos, bit_pos;
  174. k = 0;
  175. while (1) {
  176. config_reg = pfc->info->cfg_regs + k;
  177. r_width = config_reg->reg_width;
  178. f_width = config_reg->field_width;
  179. if (!r_width)
  180. break;
  181. pos = 0;
  182. m = 0;
  183. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  184. if (f_width)
  185. curr_width = f_width;
  186. else
  187. curr_width = config_reg->var_field_width[m];
  188. ncomb = 1 << curr_width;
  189. for (n = 0; n < ncomb; n++) {
  190. if (config_reg->enum_ids[pos + n] == enum_id) {
  191. *crp = config_reg;
  192. *fieldp = m;
  193. *valuep = n;
  194. *cntp = &config_reg->cnt[m];
  195. return 0;
  196. }
  197. }
  198. pos += ncomb;
  199. m++;
  200. }
  201. k++;
  202. }
  203. return -1;
  204. }
  205. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
  206. pinmux_enum_t *enum_idp)
  207. {
  208. pinmux_enum_t *data = pfc->info->gpio_data;
  209. int k;
  210. if (pos) {
  211. *enum_idp = data[pos + 1];
  212. return pos + 1;
  213. }
  214. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  215. if (data[k] == mark) {
  216. *enum_idp = data[k + 1];
  217. return k + 1;
  218. }
  219. }
  220. pr_err("cannot locate data/mark enum_id for mark %d\n", mark);
  221. return -1;
  222. }
  223. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type,
  224. int cfg_mode)
  225. {
  226. struct pinmux_cfg_reg *cr = NULL;
  227. pinmux_enum_t enum_id;
  228. struct pinmux_range *range;
  229. int in_range, pos, field, value;
  230. unsigned long *cntp;
  231. switch (pinmux_type) {
  232. case PINMUX_TYPE_FUNCTION:
  233. range = NULL;
  234. break;
  235. case PINMUX_TYPE_OUTPUT:
  236. range = &pfc->info->output;
  237. break;
  238. case PINMUX_TYPE_INPUT:
  239. range = &pfc->info->input;
  240. break;
  241. case PINMUX_TYPE_INPUT_PULLUP:
  242. range = &pfc->info->input_pu;
  243. break;
  244. case PINMUX_TYPE_INPUT_PULLDOWN:
  245. range = &pfc->info->input_pd;
  246. break;
  247. default:
  248. goto out_err;
  249. }
  250. pos = 0;
  251. enum_id = 0;
  252. field = 0;
  253. value = 0;
  254. while (1) {
  255. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  256. if (pos <= 0)
  257. goto out_err;
  258. if (!enum_id)
  259. break;
  260. /* first check if this is a function enum */
  261. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  262. if (!in_range) {
  263. /* not a function enum */
  264. if (range) {
  265. /*
  266. * other range exists, so this pin is
  267. * a regular GPIO pin that now is being
  268. * bound to a specific direction.
  269. *
  270. * for this case we only allow function enums
  271. * and the enums that match the other range.
  272. */
  273. in_range = sh_pfc_enum_in_range(enum_id, range);
  274. /*
  275. * special case pass through for fixed
  276. * input-only or output-only pins without
  277. * function enum register association.
  278. */
  279. if (in_range && enum_id == range->force)
  280. continue;
  281. } else {
  282. /*
  283. * no other range exists, so this pin
  284. * must then be of the function type.
  285. *
  286. * allow function type pins to select
  287. * any combination of function/in/out
  288. * in their MARK lists.
  289. */
  290. in_range = 1;
  291. }
  292. }
  293. if (!in_range)
  294. continue;
  295. if (sh_pfc_get_config_reg(pfc, enum_id, &cr,
  296. &field, &value, &cntp) != 0)
  297. goto out_err;
  298. switch (cfg_mode) {
  299. case GPIO_CFG_DRYRUN:
  300. if (!*cntp ||
  301. (sh_pfc_read_config_reg(pfc, cr, field) != value))
  302. continue;
  303. break;
  304. case GPIO_CFG_REQ:
  305. sh_pfc_write_config_reg(pfc, cr, field, value);
  306. *cntp = *cntp + 1;
  307. break;
  308. case GPIO_CFG_FREE:
  309. *cntp = *cntp - 1;
  310. break;
  311. }
  312. }
  313. return 0;
  314. out_err:
  315. return -1;
  316. }
  317. static int sh_pfc_probe(struct platform_device *pdev)
  318. {
  319. struct sh_pfc_soc_info *info;
  320. struct sh_pfc *pfc;
  321. int ret;
  322. info = pdev->id_entry->driver_data
  323. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  324. if (info == NULL)
  325. return -ENODEV;
  326. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  327. if (pfc == NULL)
  328. return -ENOMEM;
  329. pfc->info = info;
  330. pfc->dev = &pdev->dev;
  331. ret = sh_pfc_ioremap(pfc, pdev);
  332. if (unlikely(ret < 0))
  333. return ret;
  334. spin_lock_init(&pfc->lock);
  335. pinctrl_provide_dummies();
  336. /*
  337. * Initialize pinctrl bindings first
  338. */
  339. ret = sh_pfc_register_pinctrl(pfc);
  340. if (unlikely(ret != 0))
  341. return ret;
  342. #ifdef CONFIG_GPIO_SH_PFC
  343. /*
  344. * Then the GPIO chip
  345. */
  346. ret = sh_pfc_register_gpiochip(pfc);
  347. if (unlikely(ret != 0)) {
  348. /*
  349. * If the GPIO chip fails to come up we still leave the
  350. * PFC state as it is, given that there are already
  351. * extant users of it that have succeeded by this point.
  352. */
  353. pr_notice("failed to init GPIO chip, ignoring...\n");
  354. }
  355. #endif
  356. platform_set_drvdata(pdev, pfc);
  357. pr_info("%s support registered\n", info->name);
  358. return 0;
  359. }
  360. static int sh_pfc_remove(struct platform_device *pdev)
  361. {
  362. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  363. #ifdef CONFIG_GPIO_SH_PFC
  364. sh_pfc_unregister_gpiochip(pfc);
  365. #endif
  366. sh_pfc_unregister_pinctrl(pfc);
  367. platform_set_drvdata(pdev, NULL);
  368. return 0;
  369. }
  370. static const struct platform_device_id sh_pfc_id_table[] = {
  371. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  372. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  373. #endif
  374. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  375. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  376. #endif
  377. #ifdef CONFIG_PINCTRL_PFC_SH7203
  378. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  379. #endif
  380. #ifdef CONFIG_PINCTRL_PFC_SH7264
  381. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  382. #endif
  383. #ifdef CONFIG_PINCTRL_PFC_SH7269
  384. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  385. #endif
  386. #ifdef CONFIG_PINCTRL_PFC_SH7372
  387. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  388. #endif
  389. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  390. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  391. #endif
  392. #ifdef CONFIG_PINCTRL_PFC_SH7720
  393. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  394. #endif
  395. #ifdef CONFIG_PINCTRL_PFC_SH7722
  396. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  397. #endif
  398. #ifdef CONFIG_PINCTRL_PFC_SH7723
  399. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  400. #endif
  401. #ifdef CONFIG_PINCTRL_PFC_SH7724
  402. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  403. #endif
  404. #ifdef CONFIG_PINCTRL_PFC_SH7734
  405. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  406. #endif
  407. #ifdef CONFIG_PINCTRL_PFC_SH7757
  408. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  409. #endif
  410. #ifdef CONFIG_PINCTRL_PFC_SH7785
  411. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  412. #endif
  413. #ifdef CONFIG_PINCTRL_PFC_SH7786
  414. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  415. #endif
  416. #ifdef CONFIG_PINCTRL_PFC_SHX3
  417. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  418. #endif
  419. { "sh-pfc", 0 },
  420. { },
  421. };
  422. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  423. static struct platform_driver sh_pfc_driver = {
  424. .probe = sh_pfc_probe,
  425. .remove = sh_pfc_remove,
  426. .id_table = sh_pfc_id_table,
  427. .driver = {
  428. .name = DRV_NAME,
  429. .owner = THIS_MODULE,
  430. },
  431. };
  432. static int __init sh_pfc_init(void)
  433. {
  434. return platform_driver_register(&sh_pfc_driver);
  435. }
  436. postcore_initcall(sh_pfc_init);
  437. static void __exit sh_pfc_exit(void)
  438. {
  439. platform_driver_unregister(&sh_pfc_driver);
  440. }
  441. module_exit(sh_pfc_exit);
  442. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  443. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  444. MODULE_LICENSE("GPL v2");