core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sh_pfc.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/bitops.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioport.h>
  21. static struct sh_pfc *sh_pfc __read_mostly;
  22. static inline bool sh_pfc_initialized(void)
  23. {
  24. return !!sh_pfc;
  25. }
  26. static void pfc_iounmap(struct sh_pfc *pfc)
  27. {
  28. int k;
  29. for (k = 0; k < pfc->num_resources; k++)
  30. if (pfc->window[k].virt)
  31. iounmap(pfc->window[k].virt);
  32. kfree(pfc->window);
  33. pfc->window = NULL;
  34. }
  35. static int pfc_ioremap(struct sh_pfc *pfc)
  36. {
  37. struct resource *res;
  38. int k;
  39. if (!pfc->num_resources)
  40. return 0;
  41. pfc->window = kzalloc(pfc->num_resources * sizeof(*pfc->window),
  42. GFP_NOWAIT);
  43. if (!pfc->window)
  44. goto err1;
  45. for (k = 0; k < pfc->num_resources; k++) {
  46. res = pfc->resource + k;
  47. WARN_ON(resource_type(res) != IORESOURCE_MEM);
  48. pfc->window[k].phys = res->start;
  49. pfc->window[k].size = resource_size(res);
  50. pfc->window[k].virt = ioremap_nocache(res->start,
  51. resource_size(res));
  52. if (!pfc->window[k].virt)
  53. goto err2;
  54. }
  55. return 0;
  56. err2:
  57. pfc_iounmap(pfc);
  58. err1:
  59. return -1;
  60. }
  61. static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
  62. unsigned long address)
  63. {
  64. struct pfc_window *window;
  65. int k;
  66. /* scan through physical windows and convert address */
  67. for (k = 0; k < pfc->num_resources; k++) {
  68. window = pfc->window + k;
  69. if (address < window->phys)
  70. continue;
  71. if (address >= (window->phys + window->size))
  72. continue;
  73. return window->virt + (address - window->phys);
  74. }
  75. /* no windows defined, register must be 1:1 mapped virt:phys */
  76. return (void __iomem *)address;
  77. }
  78. static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  79. {
  80. if (enum_id < r->begin)
  81. return 0;
  82. if (enum_id > r->end)
  83. return 0;
  84. return 1;
  85. }
  86. static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
  87. unsigned long reg_width)
  88. {
  89. switch (reg_width) {
  90. case 8:
  91. return ioread8(mapped_reg);
  92. case 16:
  93. return ioread16(mapped_reg);
  94. case 32:
  95. return ioread32(mapped_reg);
  96. }
  97. BUG();
  98. return 0;
  99. }
  100. static void gpio_write_raw_reg(void __iomem *mapped_reg,
  101. unsigned long reg_width,
  102. unsigned long data)
  103. {
  104. switch (reg_width) {
  105. case 8:
  106. iowrite8(data, mapped_reg);
  107. return;
  108. case 16:
  109. iowrite16(data, mapped_reg);
  110. return;
  111. case 32:
  112. iowrite32(data, mapped_reg);
  113. return;
  114. }
  115. BUG();
  116. }
  117. int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
  118. {
  119. unsigned long pos;
  120. pos = dr->reg_width - (in_pos + 1);
  121. pr_debug("read_bit: addr = %lx, pos = %ld, "
  122. "r_width = %ld\n", dr->reg, pos, dr->reg_width);
  123. return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
  124. }
  125. EXPORT_SYMBOL_GPL(sh_pfc_read_bit);
  126. void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
  127. unsigned long value)
  128. {
  129. unsigned long pos;
  130. pos = dr->reg_width - (in_pos + 1);
  131. pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
  132. "r_width = %ld\n",
  133. dr->reg, !!value, pos, dr->reg_width);
  134. if (value)
  135. set_bit(pos, &dr->reg_shadow);
  136. else
  137. clear_bit(pos, &dr->reg_shadow);
  138. gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
  139. }
  140. EXPORT_SYMBOL_GPL(sh_pfc_write_bit);
  141. static void config_reg_helper(struct sh_pfc *pfc,
  142. struct pinmux_cfg_reg *crp,
  143. unsigned long in_pos,
  144. void __iomem **mapped_regp,
  145. unsigned long *maskp,
  146. unsigned long *posp)
  147. {
  148. int k;
  149. *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
  150. if (crp->field_width) {
  151. *maskp = (1 << crp->field_width) - 1;
  152. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  153. } else {
  154. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  155. *posp = crp->reg_width;
  156. for (k = 0; k <= in_pos; k++)
  157. *posp -= crp->var_field_width[k];
  158. }
  159. }
  160. static int read_config_reg(struct sh_pfc *pfc,
  161. struct pinmux_cfg_reg *crp,
  162. unsigned long field)
  163. {
  164. void __iomem *mapped_reg;
  165. unsigned long mask, pos;
  166. config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  167. pr_debug("read_reg: addr = %lx, field = %ld, "
  168. "r_width = %ld, f_width = %ld\n",
  169. crp->reg, field, crp->reg_width, crp->field_width);
  170. return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  171. }
  172. static void write_config_reg(struct sh_pfc *pfc,
  173. struct pinmux_cfg_reg *crp,
  174. unsigned long field, unsigned long value)
  175. {
  176. void __iomem *mapped_reg;
  177. unsigned long mask, pos, data;
  178. config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
  179. pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
  180. "r_width = %ld, f_width = %ld\n",
  181. crp->reg, value, field, crp->reg_width, crp->field_width);
  182. mask = ~(mask << pos);
  183. value = value << pos;
  184. data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
  185. data &= mask;
  186. data |= value;
  187. if (pfc->unlock_reg)
  188. gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->unlock_reg),
  189. 32, ~data);
  190. gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
  191. }
  192. static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
  193. {
  194. struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
  195. struct pinmux_data_reg *data_reg;
  196. int k, n;
  197. if (!enum_in_range(gpiop->enum_id, &pfc->data))
  198. return -1;
  199. k = 0;
  200. while (1) {
  201. data_reg = pfc->data_regs + k;
  202. if (!data_reg->reg_width)
  203. break;
  204. data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
  205. for (n = 0; n < data_reg->reg_width; n++) {
  206. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  207. gpiop->flags &= ~PINMUX_FLAG_DREG;
  208. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  209. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  210. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  211. return 0;
  212. }
  213. }
  214. k++;
  215. }
  216. BUG();
  217. return -1;
  218. }
  219. static void setup_data_regs(struct sh_pfc *pfc)
  220. {
  221. struct pinmux_data_reg *drp;
  222. int k;
  223. for (k = pfc->first_gpio; k <= pfc->last_gpio; k++)
  224. setup_data_reg(pfc, k);
  225. k = 0;
  226. while (1) {
  227. drp = pfc->data_regs + k;
  228. if (!drp->reg_width)
  229. break;
  230. drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
  231. drp->reg_width);
  232. k++;
  233. }
  234. }
  235. int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
  236. struct pinmux_data_reg **drp, int *bitp)
  237. {
  238. struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
  239. int k, n;
  240. if (!enum_in_range(gpiop->enum_id, &pfc->data))
  241. return -1;
  242. k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
  243. n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  244. *drp = pfc->data_regs + k;
  245. *bitp = n;
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg);
  249. static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
  250. struct pinmux_cfg_reg **crp,
  251. int *fieldp, int *valuep,
  252. unsigned long **cntp)
  253. {
  254. struct pinmux_cfg_reg *config_reg;
  255. unsigned long r_width, f_width, curr_width, ncomb;
  256. int k, m, n, pos, bit_pos;
  257. k = 0;
  258. while (1) {
  259. config_reg = pfc->cfg_regs + k;
  260. r_width = config_reg->reg_width;
  261. f_width = config_reg->field_width;
  262. if (!r_width)
  263. break;
  264. pos = 0;
  265. m = 0;
  266. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  267. if (f_width)
  268. curr_width = f_width;
  269. else
  270. curr_width = config_reg->var_field_width[m];
  271. ncomb = 1 << curr_width;
  272. for (n = 0; n < ncomb; n++) {
  273. if (config_reg->enum_ids[pos + n] == enum_id) {
  274. *crp = config_reg;
  275. *fieldp = m;
  276. *valuep = n;
  277. *cntp = &config_reg->cnt[m];
  278. return 0;
  279. }
  280. }
  281. pos += ncomb;
  282. m++;
  283. }
  284. k++;
  285. }
  286. return -1;
  287. }
  288. int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
  289. pinmux_enum_t *enum_idp)
  290. {
  291. pinmux_enum_t enum_id = pfc->gpios[gpio].enum_id;
  292. pinmux_enum_t *data = pfc->gpio_data;
  293. int k;
  294. if (!enum_in_range(enum_id, &pfc->data)) {
  295. if (!enum_in_range(enum_id, &pfc->mark)) {
  296. pr_err("non data/mark enum_id for gpio %d\n", gpio);
  297. return -1;
  298. }
  299. }
  300. if (pos) {
  301. *enum_idp = data[pos + 1];
  302. return pos + 1;
  303. }
  304. for (k = 0; k < pfc->gpio_data_size; k++) {
  305. if (data[k] == enum_id) {
  306. *enum_idp = data[k + 1];
  307. return k + 1;
  308. }
  309. }
  310. pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
  311. return -1;
  312. }
  313. EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum);
  314. int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
  315. int cfg_mode)
  316. {
  317. struct pinmux_cfg_reg *cr = NULL;
  318. pinmux_enum_t enum_id;
  319. struct pinmux_range *range;
  320. int in_range, pos, field, value;
  321. unsigned long *cntp;
  322. switch (pinmux_type) {
  323. case PINMUX_TYPE_FUNCTION:
  324. range = NULL;
  325. break;
  326. case PINMUX_TYPE_OUTPUT:
  327. range = &pfc->output;
  328. break;
  329. case PINMUX_TYPE_INPUT:
  330. range = &pfc->input;
  331. break;
  332. case PINMUX_TYPE_INPUT_PULLUP:
  333. range = &pfc->input_pu;
  334. break;
  335. case PINMUX_TYPE_INPUT_PULLDOWN:
  336. range = &pfc->input_pd;
  337. break;
  338. default:
  339. goto out_err;
  340. }
  341. pos = 0;
  342. enum_id = 0;
  343. field = 0;
  344. value = 0;
  345. while (1) {
  346. pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
  347. if (pos <= 0)
  348. goto out_err;
  349. if (!enum_id)
  350. break;
  351. /* first check if this is a function enum */
  352. in_range = enum_in_range(enum_id, &pfc->function);
  353. if (!in_range) {
  354. /* not a function enum */
  355. if (range) {
  356. /*
  357. * other range exists, so this pin is
  358. * a regular GPIO pin that now is being
  359. * bound to a specific direction.
  360. *
  361. * for this case we only allow function enums
  362. * and the enums that match the other range.
  363. */
  364. in_range = enum_in_range(enum_id, range);
  365. /*
  366. * special case pass through for fixed
  367. * input-only or output-only pins without
  368. * function enum register association.
  369. */
  370. if (in_range && enum_id == range->force)
  371. continue;
  372. } else {
  373. /*
  374. * no other range exists, so this pin
  375. * must then be of the function type.
  376. *
  377. * allow function type pins to select
  378. * any combination of function/in/out
  379. * in their MARK lists.
  380. */
  381. in_range = 1;
  382. }
  383. }
  384. if (!in_range)
  385. continue;
  386. if (get_config_reg(pfc, enum_id, &cr,
  387. &field, &value, &cntp) != 0)
  388. goto out_err;
  389. switch (cfg_mode) {
  390. case GPIO_CFG_DRYRUN:
  391. if (!*cntp ||
  392. (read_config_reg(pfc, cr, field) != value))
  393. continue;
  394. break;
  395. case GPIO_CFG_REQ:
  396. write_config_reg(pfc, cr, field, value);
  397. *cntp = *cntp + 1;
  398. break;
  399. case GPIO_CFG_FREE:
  400. *cntp = *cntp - 1;
  401. break;
  402. }
  403. }
  404. return 0;
  405. out_err:
  406. return -1;
  407. }
  408. EXPORT_SYMBOL_GPL(sh_pfc_config_gpio);
  409. int sh_pfc_set_direction(struct sh_pfc *pfc, unsigned gpio,
  410. int new_pinmux_type)
  411. {
  412. int pinmux_type;
  413. int ret = -EINVAL;
  414. if (!pfc)
  415. goto err_out;
  416. pinmux_type = pfc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
  417. switch (pinmux_type) {
  418. case PINMUX_TYPE_GPIO:
  419. break;
  420. case PINMUX_TYPE_OUTPUT:
  421. case PINMUX_TYPE_INPUT:
  422. case PINMUX_TYPE_INPUT_PULLUP:
  423. case PINMUX_TYPE_INPUT_PULLDOWN:
  424. sh_pfc_config_gpio(pfc, gpio, pinmux_type, GPIO_CFG_FREE);
  425. break;
  426. default:
  427. goto err_out;
  428. }
  429. if (sh_pfc_config_gpio(pfc, gpio,
  430. new_pinmux_type,
  431. GPIO_CFG_DRYRUN) != 0)
  432. goto err_out;
  433. if (sh_pfc_config_gpio(pfc, gpio,
  434. new_pinmux_type,
  435. GPIO_CFG_REQ) != 0)
  436. BUG();
  437. pfc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
  438. pfc->gpios[gpio].flags |= new_pinmux_type;
  439. ret = 0;
  440. err_out:
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(sh_pfc_set_direction);
  444. int register_sh_pfc(struct sh_pfc *pfc)
  445. {
  446. int (*initroutine)(struct sh_pfc *) = NULL;
  447. int ret;
  448. /*
  449. * Ensure that the type encoding fits
  450. */
  451. BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
  452. if (sh_pfc)
  453. return -EBUSY;
  454. ret = pfc_ioremap(pfc);
  455. if (unlikely(ret < 0))
  456. return ret;
  457. spin_lock_init(&pfc->lock);
  458. setup_data_regs(pfc);
  459. sh_pfc = pfc;
  460. pr_info("%s support registered\n", pfc->name);
  461. initroutine = symbol_request(sh_pfc_register_gpiochip);
  462. if (initroutine) {
  463. (*initroutine)(pfc);
  464. symbol_put_addr(initroutine);
  465. }
  466. return 0;
  467. }