core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc,
  49. unsigned long address)
  50. {
  51. struct sh_pfc_window *window;
  52. int k;
  53. /* scan through physical windows and convert address */
  54. for (k = 0; k < pfc->num_windows; k++) {
  55. window = pfc->window + k;
  56. if (address < window->phys)
  57. continue;
  58. if (address >= (window->phys + window->size))
  59. continue;
  60. return window->virt + (address - window->phys);
  61. }
  62. /* no windows defined, register must be 1:1 mapped virt:phys */
  63. return (void __iomem *)address;
  64. }
  65. struct sh_pfc_pin *sh_pfc_get_pin(struct sh_pfc *pfc, unsigned int pin)
  66. {
  67. return &pfc->info->pins[pin];
  68. }
  69. static int sh_pfc_enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  70. {
  71. if (enum_id < r->begin)
  72. return 0;
  73. if (enum_id > r->end)
  74. return 0;
  75. return 1;
  76. }
  77. static unsigned long sh_pfc_read_raw_reg(void __iomem *mapped_reg,
  78. unsigned long reg_width)
  79. {
  80. switch (reg_width) {
  81. case 8:
  82. return ioread8(mapped_reg);
  83. case 16:
  84. return ioread16(mapped_reg);
  85. case 32:
  86. return ioread32(mapped_reg);
  87. }
  88. BUG();
  89. return 0;
  90. }
  91. static void sh_pfc_write_raw_reg(void __iomem *mapped_reg,
  92. unsigned long reg_width, unsigned long data)
  93. {
  94. switch (reg_width) {
  95. case 8:
  96. iowrite8(data, mapped_reg);
  97. return;
  98. case 16:
  99. iowrite16(data, mapped_reg);
  100. return;
  101. case 32:
  102. iowrite32(data, mapped_reg);
  103. return;
  104. }
  105. BUG();
  106. }
  107. int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
  108. {
  109. unsigned long pos;
  110. pos = dr->reg_width - (in_pos + 1);
  111. pr_debug("read_bit: addr = %lx, pos = %ld, "
  112. "r_width = %ld\n", dr->reg, pos, dr->reg_width);
  113. return (sh_pfc_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
  114. }
  115. void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
  116. unsigned long value)
  117. {
  118. unsigned long pos;
  119. pos = dr->reg_width - (in_pos + 1);
  120. pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
  121. "r_width = %ld\n",
  122. dr->reg, !!value, pos, dr->reg_width);
  123. if (value)
  124. set_bit(pos, &dr->reg_shadow);
  125. else
  126. clear_bit(pos, &dr->reg_shadow);
  127. sh_pfc_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
  128. }
  129. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  130. struct pinmux_cfg_reg *crp,
  131. unsigned long in_pos,
  132. void __iomem **mapped_regp,
  133. unsigned long *maskp,
  134. unsigned long *posp)
  135. {
  136. int k;
  137. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  138. if (crp->field_width) {
  139. *maskp = (1 << crp->field_width) - 1;
  140. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  141. } else {
  142. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  143. *posp = crp->reg_width;
  144. for (k = 0; k <= in_pos; k++)
  145. *posp -= crp->var_field_width[k];
  146. }
  147. }
  148. static int sh_pfc_read_config_reg(struct sh_pfc *pfc,
  149. struct pinmux_cfg_reg *crp,
  150. unsigned long field)
  151. {
  152. void __iomem *mapped_reg;
  153. unsigned long mask, pos;
  154. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  155. pr_debug("read_reg: addr = %lx, field = %ld, "
  156. "r_width = %ld, f_width = %ld\n",
  157. crp->reg, field, crp->reg_width, crp->field_width);
  158. return (sh_pfc_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  159. }
  160. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  161. struct pinmux_cfg_reg *crp,
  162. unsigned long field, unsigned long value)
  163. {
  164. void __iomem *mapped_reg;
  165. unsigned long mask, pos, data;
  166. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  167. pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
  168. "r_width = %ld, f_width = %ld\n",
  169. crp->reg, value, field, crp->reg_width, crp->field_width);
  170. mask = ~(mask << pos);
  171. value = value << pos;
  172. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  173. data &= mask;
  174. data |= value;
  175. if (pfc->info->unlock_reg)
  176. sh_pfc_write_raw_reg(
  177. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  178. ~data);
  179. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  180. }
  181. static void sh_pfc_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
  182. {
  183. struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio];
  184. struct pinmux_data_reg *data_reg;
  185. int k, n;
  186. k = 0;
  187. while (1) {
  188. data_reg = pfc->info->data_regs + k;
  189. if (!data_reg->reg_width)
  190. break;
  191. data_reg->mapped_reg = sh_pfc_phys_to_virt(pfc, data_reg->reg);
  192. for (n = 0; n < data_reg->reg_width; n++) {
  193. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  194. gpiop->flags &= ~PINMUX_FLAG_DREG;
  195. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  196. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  197. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  198. return;
  199. }
  200. }
  201. k++;
  202. }
  203. BUG();
  204. }
  205. static void sh_pfc_setup_data_regs(struct sh_pfc *pfc)
  206. {
  207. struct pinmux_data_reg *drp;
  208. int k;
  209. for (k = 0; k < pfc->info->nr_pins; k++) {
  210. if (pfc->info->pins[k].enum_id == 0)
  211. continue;
  212. sh_pfc_setup_data_reg(pfc, k);
  213. }
  214. k = 0;
  215. while (1) {
  216. drp = pfc->info->data_regs + k;
  217. if (!drp->reg_width)
  218. break;
  219. drp->reg_shadow = sh_pfc_read_raw_reg(drp->mapped_reg,
  220. drp->reg_width);
  221. k++;
  222. }
  223. }
  224. void sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
  225. struct pinmux_data_reg **drp, int *bitp)
  226. {
  227. struct sh_pfc_pin *gpiop = sh_pfc_get_pin(pfc, gpio);
  228. int k, n;
  229. k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
  230. n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  231. *drp = pfc->info->data_regs + k;
  232. *bitp = n;
  233. }
  234. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  235. struct pinmux_cfg_reg **crp, int *fieldp,
  236. int *valuep, unsigned long **cntp)
  237. {
  238. struct pinmux_cfg_reg *config_reg;
  239. unsigned long r_width, f_width, curr_width, ncomb;
  240. int k, m, n, pos, bit_pos;
  241. k = 0;
  242. while (1) {
  243. config_reg = pfc->info->cfg_regs + k;
  244. r_width = config_reg->reg_width;
  245. f_width = config_reg->field_width;
  246. if (!r_width)
  247. break;
  248. pos = 0;
  249. m = 0;
  250. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  251. if (f_width)
  252. curr_width = f_width;
  253. else
  254. curr_width = config_reg->var_field_width[m];
  255. ncomb = 1 << curr_width;
  256. for (n = 0; n < ncomb; n++) {
  257. if (config_reg->enum_ids[pos + n] == enum_id) {
  258. *crp = config_reg;
  259. *fieldp = m;
  260. *valuep = n;
  261. *cntp = &config_reg->cnt[m];
  262. return 0;
  263. }
  264. }
  265. pos += ncomb;
  266. m++;
  267. }
  268. k++;
  269. }
  270. return -1;
  271. }
  272. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, pinmux_enum_t mark, int pos,
  273. pinmux_enum_t *enum_idp)
  274. {
  275. pinmux_enum_t *data = pfc->info->gpio_data;
  276. int k;
  277. if (pos) {
  278. *enum_idp = data[pos + 1];
  279. return pos + 1;
  280. }
  281. for (k = 0; k < pfc->info->gpio_data_size; k++) {
  282. if (data[k] == mark) {
  283. *enum_idp = data[k + 1];
  284. return k + 1;
  285. }
  286. }
  287. pr_err("cannot locate data/mark enum_id for mark %d\n", mark);
  288. return -1;
  289. }
  290. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type,
  291. int cfg_mode)
  292. {
  293. struct pinmux_cfg_reg *cr = NULL;
  294. pinmux_enum_t enum_id;
  295. struct pinmux_range *range;
  296. int in_range, pos, field, value;
  297. unsigned long *cntp;
  298. switch (pinmux_type) {
  299. case PINMUX_TYPE_FUNCTION:
  300. range = NULL;
  301. break;
  302. case PINMUX_TYPE_OUTPUT:
  303. range = &pfc->info->output;
  304. break;
  305. case PINMUX_TYPE_INPUT:
  306. range = &pfc->info->input;
  307. break;
  308. case PINMUX_TYPE_INPUT_PULLUP:
  309. range = &pfc->info->input_pu;
  310. break;
  311. case PINMUX_TYPE_INPUT_PULLDOWN:
  312. range = &pfc->info->input_pd;
  313. break;
  314. default:
  315. goto out_err;
  316. }
  317. pos = 0;
  318. enum_id = 0;
  319. field = 0;
  320. value = 0;
  321. while (1) {
  322. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  323. if (pos <= 0)
  324. goto out_err;
  325. if (!enum_id)
  326. break;
  327. /* first check if this is a function enum */
  328. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  329. if (!in_range) {
  330. /* not a function enum */
  331. if (range) {
  332. /*
  333. * other range exists, so this pin is
  334. * a regular GPIO pin that now is being
  335. * bound to a specific direction.
  336. *
  337. * for this case we only allow function enums
  338. * and the enums that match the other range.
  339. */
  340. in_range = sh_pfc_enum_in_range(enum_id, range);
  341. /*
  342. * special case pass through for fixed
  343. * input-only or output-only pins without
  344. * function enum register association.
  345. */
  346. if (in_range && enum_id == range->force)
  347. continue;
  348. } else {
  349. /*
  350. * no other range exists, so this pin
  351. * must then be of the function type.
  352. *
  353. * allow function type pins to select
  354. * any combination of function/in/out
  355. * in their MARK lists.
  356. */
  357. in_range = 1;
  358. }
  359. }
  360. if (!in_range)
  361. continue;
  362. if (sh_pfc_get_config_reg(pfc, enum_id, &cr,
  363. &field, &value, &cntp) != 0)
  364. goto out_err;
  365. switch (cfg_mode) {
  366. case GPIO_CFG_DRYRUN:
  367. if (!*cntp ||
  368. (sh_pfc_read_config_reg(pfc, cr, field) != value))
  369. continue;
  370. break;
  371. case GPIO_CFG_REQ:
  372. sh_pfc_write_config_reg(pfc, cr, field, value);
  373. *cntp = *cntp + 1;
  374. break;
  375. case GPIO_CFG_FREE:
  376. *cntp = *cntp - 1;
  377. break;
  378. }
  379. }
  380. return 0;
  381. out_err:
  382. return -1;
  383. }
  384. static int sh_pfc_probe(struct platform_device *pdev)
  385. {
  386. struct sh_pfc_soc_info *info;
  387. struct sh_pfc *pfc;
  388. int ret;
  389. /*
  390. * Ensure that the type encoding fits
  391. */
  392. BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
  393. info = pdev->id_entry->driver_data
  394. ? (void *)pdev->id_entry->driver_data : pdev->dev.platform_data;
  395. if (info == NULL)
  396. return -ENODEV;
  397. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  398. if (pfc == NULL)
  399. return -ENOMEM;
  400. pfc->info = info;
  401. pfc->dev = &pdev->dev;
  402. ret = sh_pfc_ioremap(pfc, pdev);
  403. if (unlikely(ret < 0))
  404. return ret;
  405. spin_lock_init(&pfc->lock);
  406. pinctrl_provide_dummies();
  407. sh_pfc_setup_data_regs(pfc);
  408. /*
  409. * Initialize pinctrl bindings first
  410. */
  411. ret = sh_pfc_register_pinctrl(pfc);
  412. if (unlikely(ret != 0))
  413. return ret;
  414. #ifdef CONFIG_GPIO_SH_PFC
  415. /*
  416. * Then the GPIO chip
  417. */
  418. ret = sh_pfc_register_gpiochip(pfc);
  419. if (unlikely(ret != 0)) {
  420. /*
  421. * If the GPIO chip fails to come up we still leave the
  422. * PFC state as it is, given that there are already
  423. * extant users of it that have succeeded by this point.
  424. */
  425. pr_notice("failed to init GPIO chip, ignoring...\n");
  426. }
  427. #endif
  428. platform_set_drvdata(pdev, pfc);
  429. pr_info("%s support registered\n", info->name);
  430. return 0;
  431. }
  432. static int sh_pfc_remove(struct platform_device *pdev)
  433. {
  434. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  435. #ifdef CONFIG_GPIO_SH_PFC
  436. sh_pfc_unregister_gpiochip(pfc);
  437. #endif
  438. sh_pfc_unregister_pinctrl(pfc);
  439. platform_set_drvdata(pdev, NULL);
  440. return 0;
  441. }
  442. static const struct platform_device_id sh_pfc_id_table[] = {
  443. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  444. { "pfc-r8a7740", (kernel_ulong_t)&r8a7740_pinmux_info },
  445. #endif
  446. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  447. { "pfc-r8a7779", (kernel_ulong_t)&r8a7779_pinmux_info },
  448. #endif
  449. #ifdef CONFIG_PINCTRL_PFC_SH7203
  450. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  451. #endif
  452. #ifdef CONFIG_PINCTRL_PFC_SH7264
  453. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  454. #endif
  455. #ifdef CONFIG_PINCTRL_PFC_SH7269
  456. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  457. #endif
  458. #ifdef CONFIG_PINCTRL_PFC_SH7372
  459. { "pfc-sh7372", (kernel_ulong_t)&sh7372_pinmux_info },
  460. #endif
  461. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  462. { "pfc-sh73a0", (kernel_ulong_t)&sh73a0_pinmux_info },
  463. #endif
  464. #ifdef CONFIG_PINCTRL_PFC_SH7720
  465. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  466. #endif
  467. #ifdef CONFIG_PINCTRL_PFC_SH7722
  468. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  469. #endif
  470. #ifdef CONFIG_PINCTRL_PFC_SH7723
  471. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  472. #endif
  473. #ifdef CONFIG_PINCTRL_PFC_SH7724
  474. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  475. #endif
  476. #ifdef CONFIG_PINCTRL_PFC_SH7734
  477. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  478. #endif
  479. #ifdef CONFIG_PINCTRL_PFC_SH7757
  480. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  481. #endif
  482. #ifdef CONFIG_PINCTRL_PFC_SH7785
  483. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  484. #endif
  485. #ifdef CONFIG_PINCTRL_PFC_SH7786
  486. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  487. #endif
  488. #ifdef CONFIG_PINCTRL_PFC_SHX3
  489. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  490. #endif
  491. { "sh-pfc", 0 },
  492. { },
  493. };
  494. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  495. static struct platform_driver sh_pfc_driver = {
  496. .probe = sh_pfc_probe,
  497. .remove = sh_pfc_remove,
  498. .id_table = sh_pfc_id_table,
  499. .driver = {
  500. .name = DRV_NAME,
  501. .owner = THIS_MODULE,
  502. },
  503. };
  504. static int __init sh_pfc_init(void)
  505. {
  506. return platform_driver_register(&sh_pfc_driver);
  507. }
  508. postcore_initcall(sh_pfc_init);
  509. static void __exit sh_pfc_exit(void)
  510. {
  511. platform_driver_unregister(&sh_pfc_driver);
  512. }
  513. module_exit(sh_pfc_exit);
  514. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  515. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  516. MODULE_LICENSE("GPL v2");