core.c 14 KB

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