core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #include <linux/bitops.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pinctrl/machine.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include "core.h"
  25. static int sh_pfc_map_resources(struct sh_pfc *pfc,
  26. struct platform_device *pdev)
  27. {
  28. unsigned int num_windows, num_irqs;
  29. struct sh_pfc_window *windows;
  30. unsigned int *irqs = NULL;
  31. struct resource *res;
  32. unsigned int i;
  33. int irq;
  34. /* Count the MEM and IRQ resources. */
  35. for (num_windows = 0;; num_windows++) {
  36. res = platform_get_resource(pdev, IORESOURCE_MEM, num_windows);
  37. if (!res)
  38. break;
  39. }
  40. for (num_irqs = 0;; num_irqs++) {
  41. irq = platform_get_irq(pdev, num_irqs);
  42. if (irq == -EPROBE_DEFER)
  43. return irq;
  44. if (irq < 0)
  45. break;
  46. }
  47. if (num_windows == 0)
  48. return -EINVAL;
  49. /* Allocate memory windows and IRQs arrays. */
  50. windows = devm_kzalloc(pfc->dev, num_windows * sizeof(*windows),
  51. GFP_KERNEL);
  52. if (windows == NULL)
  53. return -ENOMEM;
  54. pfc->num_windows = num_windows;
  55. pfc->windows = windows;
  56. if (num_irqs) {
  57. irqs = devm_kzalloc(pfc->dev, num_irqs * sizeof(*irqs),
  58. GFP_KERNEL);
  59. if (irqs == NULL)
  60. return -ENOMEM;
  61. pfc->num_irqs = num_irqs;
  62. pfc->irqs = irqs;
  63. }
  64. /* Fill them. */
  65. for (i = 0; i < num_windows; i++) {
  66. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  67. windows->phys = res->start;
  68. windows->size = resource_size(res);
  69. windows->virt = devm_ioremap_resource(pfc->dev, res);
  70. if (IS_ERR(windows->virt))
  71. return -ENOMEM;
  72. windows++;
  73. }
  74. for (i = 0; i < num_irqs; i++)
  75. *irqs++ = platform_get_irq(pdev, i);
  76. return 0;
  77. }
  78. static void __iomem *sh_pfc_phys_to_virt(struct sh_pfc *pfc, u32 reg)
  79. {
  80. struct sh_pfc_window *window;
  81. phys_addr_t address = reg;
  82. unsigned int i;
  83. /* scan through physical windows and convert address */
  84. for (i = 0; i < pfc->num_windows; i++) {
  85. window = pfc->windows + i;
  86. if (address < window->phys)
  87. continue;
  88. if (address >= (window->phys + window->size))
  89. continue;
  90. return window->virt + (address - window->phys);
  91. }
  92. BUG();
  93. return NULL;
  94. }
  95. int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
  96. {
  97. unsigned int offset;
  98. unsigned int i;
  99. for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
  100. const struct sh_pfc_pin_range *range = &pfc->ranges[i];
  101. if (pin <= range->end)
  102. return pin >= range->start
  103. ? offset + pin - range->start : -1;
  104. offset += range->end - range->start + 1;
  105. }
  106. return -EINVAL;
  107. }
  108. static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
  109. {
  110. if (enum_id < r->begin)
  111. return 0;
  112. if (enum_id > r->end)
  113. return 0;
  114. return 1;
  115. }
  116. u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
  117. {
  118. switch (reg_width) {
  119. case 8:
  120. return ioread8(mapped_reg);
  121. case 16:
  122. return ioread16(mapped_reg);
  123. case 32:
  124. return ioread32(mapped_reg);
  125. }
  126. BUG();
  127. return 0;
  128. }
  129. void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
  130. u32 data)
  131. {
  132. switch (reg_width) {
  133. case 8:
  134. iowrite8(data, mapped_reg);
  135. return;
  136. case 16:
  137. iowrite16(data, mapped_reg);
  138. return;
  139. case 32:
  140. iowrite32(data, mapped_reg);
  141. return;
  142. }
  143. BUG();
  144. }
  145. static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
  146. const struct pinmux_cfg_reg *crp,
  147. unsigned int in_pos,
  148. void __iomem **mapped_regp, u32 *maskp,
  149. unsigned int *posp)
  150. {
  151. unsigned int k;
  152. *mapped_regp = sh_pfc_phys_to_virt(pfc, crp->reg);
  153. if (crp->field_width) {
  154. *maskp = (1 << crp->field_width) - 1;
  155. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  156. } else {
  157. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  158. *posp = crp->reg_width;
  159. for (k = 0; k <= in_pos; k++)
  160. *posp -= crp->var_field_width[k];
  161. }
  162. }
  163. static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
  164. const struct pinmux_cfg_reg *crp,
  165. unsigned int field, u32 value)
  166. {
  167. void __iomem *mapped_reg;
  168. unsigned int pos;
  169. u32 mask, data;
  170. sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  171. dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
  172. "r_width = %u, f_width = %u\n",
  173. crp->reg, value, field, crp->reg_width, crp->field_width);
  174. mask = ~(mask << pos);
  175. value = value << pos;
  176. data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
  177. data &= mask;
  178. data |= value;
  179. if (pfc->info->unlock_reg)
  180. sh_pfc_write_raw_reg(
  181. sh_pfc_phys_to_virt(pfc, pfc->info->unlock_reg), 32,
  182. ~data);
  183. sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
  184. }
  185. static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
  186. const struct pinmux_cfg_reg **crp,
  187. unsigned int *fieldp, u32 *valuep)
  188. {
  189. unsigned int k = 0;
  190. while (1) {
  191. const struct pinmux_cfg_reg *config_reg =
  192. pfc->info->cfg_regs + k;
  193. unsigned int r_width = config_reg->reg_width;
  194. unsigned int f_width = config_reg->field_width;
  195. unsigned int curr_width;
  196. unsigned int bit_pos;
  197. unsigned int pos = 0;
  198. unsigned int m = 0;
  199. if (!r_width)
  200. break;
  201. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  202. u32 ncomb;
  203. u32 n;
  204. if (f_width)
  205. curr_width = f_width;
  206. else
  207. curr_width = config_reg->var_field_width[m];
  208. ncomb = 1 << curr_width;
  209. for (n = 0; n < ncomb; n++) {
  210. if (config_reg->enum_ids[pos + n] == enum_id) {
  211. *crp = config_reg;
  212. *fieldp = m;
  213. *valuep = n;
  214. return 0;
  215. }
  216. }
  217. pos += ncomb;
  218. m++;
  219. }
  220. k++;
  221. }
  222. return -EINVAL;
  223. }
  224. static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
  225. u16 *enum_idp)
  226. {
  227. const u16 *data = pfc->info->pinmux_data;
  228. unsigned int k;
  229. if (pos) {
  230. *enum_idp = data[pos + 1];
  231. return pos + 1;
  232. }
  233. for (k = 0; k < pfc->info->pinmux_data_size; k++) {
  234. if (data[k] == mark) {
  235. *enum_idp = data[k + 1];
  236. return k + 1;
  237. }
  238. }
  239. dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
  240. mark);
  241. return -EINVAL;
  242. }
  243. int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
  244. {
  245. const struct pinmux_range *range;
  246. int pos = 0;
  247. switch (pinmux_type) {
  248. case PINMUX_TYPE_GPIO:
  249. case PINMUX_TYPE_FUNCTION:
  250. range = NULL;
  251. break;
  252. case PINMUX_TYPE_OUTPUT:
  253. range = &pfc->info->output;
  254. break;
  255. case PINMUX_TYPE_INPUT:
  256. range = &pfc->info->input;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. /* Iterate over all the configuration fields we need to update. */
  262. while (1) {
  263. const struct pinmux_cfg_reg *cr;
  264. unsigned int field;
  265. u16 enum_id;
  266. u32 value;
  267. int in_range;
  268. int ret;
  269. pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
  270. if (pos < 0)
  271. return pos;
  272. if (!enum_id)
  273. break;
  274. /* Check if the configuration field selects a function. If it
  275. * doesn't, skip the field if it's not applicable to the
  276. * requested pinmux type.
  277. */
  278. in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
  279. if (!in_range) {
  280. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  281. /* Functions are allowed to modify all
  282. * fields.
  283. */
  284. in_range = 1;
  285. } else if (pinmux_type != PINMUX_TYPE_GPIO) {
  286. /* Input/output types can only modify fields
  287. * that correspond to their respective ranges.
  288. */
  289. in_range = sh_pfc_enum_in_range(enum_id, range);
  290. /*
  291. * special case pass through for fixed
  292. * input-only or output-only pins without
  293. * function enum register association.
  294. */
  295. if (in_range && enum_id == range->force)
  296. continue;
  297. }
  298. /* GPIOs are only allowed to modify function fields. */
  299. }
  300. if (!in_range)
  301. continue;
  302. ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
  303. if (ret < 0)
  304. return ret;
  305. sh_pfc_write_config_reg(pfc, cr, field, value);
  306. }
  307. return 0;
  308. }
  309. static int sh_pfc_init_ranges(struct sh_pfc *pfc)
  310. {
  311. struct sh_pfc_pin_range *range;
  312. unsigned int nr_ranges;
  313. unsigned int i;
  314. if (pfc->info->pins[0].pin == (u16)-1) {
  315. /* Pin number -1 denotes that the SoC doesn't report pin numbers
  316. * in its pin arrays yet. Consider the pin numbers range as
  317. * continuous and allocate a single range.
  318. */
  319. pfc->nr_ranges = 1;
  320. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges),
  321. GFP_KERNEL);
  322. if (pfc->ranges == NULL)
  323. return -ENOMEM;
  324. pfc->ranges->start = 0;
  325. pfc->ranges->end = pfc->info->nr_pins - 1;
  326. pfc->nr_gpio_pins = pfc->info->nr_pins;
  327. return 0;
  328. }
  329. /* Count, allocate and fill the ranges. The PFC SoC data pins array must
  330. * be sorted by pin numbers, and pins without a GPIO port must come
  331. * last.
  332. */
  333. for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
  334. if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
  335. nr_ranges++;
  336. }
  337. pfc->nr_ranges = nr_ranges;
  338. pfc->ranges = devm_kzalloc(pfc->dev, sizeof(*pfc->ranges) * nr_ranges,
  339. GFP_KERNEL);
  340. if (pfc->ranges == NULL)
  341. return -ENOMEM;
  342. range = pfc->ranges;
  343. range->start = pfc->info->pins[0].pin;
  344. for (i = 1; i < pfc->info->nr_pins; ++i) {
  345. if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
  346. continue;
  347. range->end = pfc->info->pins[i-1].pin;
  348. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  349. pfc->nr_gpio_pins = range->end + 1;
  350. range++;
  351. range->start = pfc->info->pins[i].pin;
  352. }
  353. range->end = pfc->info->pins[i-1].pin;
  354. if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
  355. pfc->nr_gpio_pins = range->end + 1;
  356. return 0;
  357. }
  358. #ifdef CONFIG_OF
  359. static const struct of_device_id sh_pfc_of_table[] = {
  360. #ifdef CONFIG_PINCTRL_PFC_EMEV2
  361. {
  362. .compatible = "renesas,pfc-emev2",
  363. .data = &emev2_pinmux_info,
  364. },
  365. #endif
  366. #ifdef CONFIG_PINCTRL_PFC_R8A73A4
  367. {
  368. .compatible = "renesas,pfc-r8a73a4",
  369. .data = &r8a73a4_pinmux_info,
  370. },
  371. #endif
  372. #ifdef CONFIG_PINCTRL_PFC_R8A7740
  373. {
  374. .compatible = "renesas,pfc-r8a7740",
  375. .data = &r8a7740_pinmux_info,
  376. },
  377. #endif
  378. #ifdef CONFIG_PINCTRL_PFC_R8A7778
  379. {
  380. .compatible = "renesas,pfc-r8a7778",
  381. .data = &r8a7778_pinmux_info,
  382. },
  383. #endif
  384. #ifdef CONFIG_PINCTRL_PFC_R8A7779
  385. {
  386. .compatible = "renesas,pfc-r8a7779",
  387. .data = &r8a7779_pinmux_info,
  388. },
  389. #endif
  390. #ifdef CONFIG_PINCTRL_PFC_R8A7790
  391. {
  392. .compatible = "renesas,pfc-r8a7790",
  393. .data = &r8a7790_pinmux_info,
  394. },
  395. #endif
  396. #ifdef CONFIG_PINCTRL_PFC_R8A7791
  397. {
  398. .compatible = "renesas,pfc-r8a7791",
  399. .data = &r8a7791_pinmux_info,
  400. },
  401. #endif
  402. #ifdef CONFIG_PINCTRL_PFC_R8A7793
  403. {
  404. .compatible = "renesas,pfc-r8a7793",
  405. .data = &r8a7793_pinmux_info,
  406. },
  407. #endif
  408. #ifdef CONFIG_PINCTRL_PFC_R8A7794
  409. {
  410. .compatible = "renesas,pfc-r8a7794",
  411. .data = &r8a7794_pinmux_info,
  412. },
  413. #endif
  414. #ifdef CONFIG_PINCTRL_PFC_R8A7795
  415. {
  416. .compatible = "renesas,pfc-r8a7795",
  417. .data = &r8a7795_pinmux_info,
  418. },
  419. #endif
  420. #ifdef CONFIG_PINCTRL_PFC_SH73A0
  421. {
  422. .compatible = "renesas,pfc-sh73a0",
  423. .data = &sh73a0_pinmux_info,
  424. },
  425. #endif
  426. { },
  427. };
  428. MODULE_DEVICE_TABLE(of, sh_pfc_of_table);
  429. #endif
  430. static int sh_pfc_probe(struct platform_device *pdev)
  431. {
  432. const struct platform_device_id *platid = platform_get_device_id(pdev);
  433. #ifdef CONFIG_OF
  434. struct device_node *np = pdev->dev.of_node;
  435. #endif
  436. const struct sh_pfc_soc_info *info;
  437. struct sh_pfc *pfc;
  438. int ret;
  439. #ifdef CONFIG_OF
  440. if (np)
  441. info = of_match_device(sh_pfc_of_table, &pdev->dev)->data;
  442. else
  443. #endif
  444. info = platid ? (const void *)platid->driver_data : NULL;
  445. if (info == NULL)
  446. return -ENODEV;
  447. pfc = devm_kzalloc(&pdev->dev, sizeof(*pfc), GFP_KERNEL);
  448. if (pfc == NULL)
  449. return -ENOMEM;
  450. pfc->info = info;
  451. pfc->dev = &pdev->dev;
  452. ret = sh_pfc_map_resources(pfc, pdev);
  453. if (unlikely(ret < 0))
  454. return ret;
  455. spin_lock_init(&pfc->lock);
  456. if (info->ops && info->ops->init) {
  457. ret = info->ops->init(pfc);
  458. if (ret < 0)
  459. return ret;
  460. /* .init() may have overridden pfc->info */
  461. info = pfc->info;
  462. }
  463. /* Enable dummy states for those platforms without pinctrl support */
  464. if (!of_have_populated_dt())
  465. pinctrl_provide_dummies();
  466. ret = sh_pfc_init_ranges(pfc);
  467. if (ret < 0)
  468. return ret;
  469. /*
  470. * Initialize pinctrl bindings first
  471. */
  472. ret = sh_pfc_register_pinctrl(pfc);
  473. if (unlikely(ret != 0))
  474. return ret;
  475. #ifdef CONFIG_GPIO_SH_PFC
  476. /*
  477. * Then the GPIO chip
  478. */
  479. ret = sh_pfc_register_gpiochip(pfc);
  480. if (unlikely(ret != 0)) {
  481. /*
  482. * If the GPIO chip fails to come up we still leave the
  483. * PFC state as it is, given that there are already
  484. * extant users of it that have succeeded by this point.
  485. */
  486. dev_notice(pfc->dev, "failed to init GPIO chip, ignoring...\n");
  487. }
  488. #endif
  489. platform_set_drvdata(pdev, pfc);
  490. dev_info(pfc->dev, "%s support registered\n", info->name);
  491. return 0;
  492. }
  493. static int sh_pfc_remove(struct platform_device *pdev)
  494. {
  495. struct sh_pfc *pfc = platform_get_drvdata(pdev);
  496. #ifdef CONFIG_GPIO_SH_PFC
  497. sh_pfc_unregister_gpiochip(pfc);
  498. #endif
  499. sh_pfc_unregister_pinctrl(pfc);
  500. return 0;
  501. }
  502. static const struct platform_device_id sh_pfc_id_table[] = {
  503. #ifdef CONFIG_PINCTRL_PFC_SH7203
  504. { "pfc-sh7203", (kernel_ulong_t)&sh7203_pinmux_info },
  505. #endif
  506. #ifdef CONFIG_PINCTRL_PFC_SH7264
  507. { "pfc-sh7264", (kernel_ulong_t)&sh7264_pinmux_info },
  508. #endif
  509. #ifdef CONFIG_PINCTRL_PFC_SH7269
  510. { "pfc-sh7269", (kernel_ulong_t)&sh7269_pinmux_info },
  511. #endif
  512. #ifdef CONFIG_PINCTRL_PFC_SH7720
  513. { "pfc-sh7720", (kernel_ulong_t)&sh7720_pinmux_info },
  514. #endif
  515. #ifdef CONFIG_PINCTRL_PFC_SH7722
  516. { "pfc-sh7722", (kernel_ulong_t)&sh7722_pinmux_info },
  517. #endif
  518. #ifdef CONFIG_PINCTRL_PFC_SH7723
  519. { "pfc-sh7723", (kernel_ulong_t)&sh7723_pinmux_info },
  520. #endif
  521. #ifdef CONFIG_PINCTRL_PFC_SH7724
  522. { "pfc-sh7724", (kernel_ulong_t)&sh7724_pinmux_info },
  523. #endif
  524. #ifdef CONFIG_PINCTRL_PFC_SH7734
  525. { "pfc-sh7734", (kernel_ulong_t)&sh7734_pinmux_info },
  526. #endif
  527. #ifdef CONFIG_PINCTRL_PFC_SH7757
  528. { "pfc-sh7757", (kernel_ulong_t)&sh7757_pinmux_info },
  529. #endif
  530. #ifdef CONFIG_PINCTRL_PFC_SH7785
  531. { "pfc-sh7785", (kernel_ulong_t)&sh7785_pinmux_info },
  532. #endif
  533. #ifdef CONFIG_PINCTRL_PFC_SH7786
  534. { "pfc-sh7786", (kernel_ulong_t)&sh7786_pinmux_info },
  535. #endif
  536. #ifdef CONFIG_PINCTRL_PFC_SHX3
  537. { "pfc-shx3", (kernel_ulong_t)&shx3_pinmux_info },
  538. #endif
  539. { "sh-pfc", 0 },
  540. { },
  541. };
  542. MODULE_DEVICE_TABLE(platform, sh_pfc_id_table);
  543. static struct platform_driver sh_pfc_driver = {
  544. .probe = sh_pfc_probe,
  545. .remove = sh_pfc_remove,
  546. .id_table = sh_pfc_id_table,
  547. .driver = {
  548. .name = DRV_NAME,
  549. .of_match_table = of_match_ptr(sh_pfc_of_table),
  550. },
  551. };
  552. static int __init sh_pfc_init(void)
  553. {
  554. return platform_driver_register(&sh_pfc_driver);
  555. }
  556. postcore_initcall(sh_pfc_init);
  557. static void __exit sh_pfc_exit(void)
  558. {
  559. platform_driver_unregister(&sh_pfc_driver);
  560. }
  561. module_exit(sh_pfc_exit);
  562. MODULE_AUTHOR("Magnus Damm, Paul Mundt, Laurent Pinchart");
  563. MODULE_DESCRIPTION("Pin Control and GPIO driver for SuperH pin function controller");
  564. MODULE_LICENSE("GPL v2");