core.c 14 KB

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