core.c 12 KB

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