sh_pfc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Pinmuxed GPIO support for SuperH.
  3. * Copy from linux kernel driver/sh/pfc.c
  4. *
  5. * Copyright (C) 2008 Magnus Damm
  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. #include <common.h>
  12. #include <asm/bitops.h>
  13. #include <asm/io.h>
  14. #include <sh_pfc.h>
  15. static struct pinmux_info *gpioc;
  16. #define pfc_phys_to_virt(p, a) ((void *)a)
  17. static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
  18. {
  19. if (enum_id < r->begin)
  20. return 0;
  21. if (enum_id > r->end)
  22. return 0;
  23. return 1;
  24. }
  25. static unsigned long gpio_read_raw_reg(void *mapped_reg,
  26. unsigned long reg_width)
  27. {
  28. switch (reg_width) {
  29. case 8:
  30. return readb(mapped_reg);
  31. case 16:
  32. return readw(mapped_reg);
  33. case 32:
  34. return readl(mapped_reg);
  35. }
  36. BUG();
  37. return 0;
  38. }
  39. static void gpio_write_raw_reg(void *mapped_reg,
  40. unsigned long reg_width,
  41. unsigned long data)
  42. {
  43. switch (reg_width) {
  44. case 8:
  45. writeb(data, mapped_reg);
  46. return;
  47. case 16:
  48. writew(data, mapped_reg);
  49. return;
  50. case 32:
  51. writel(data, mapped_reg);
  52. return;
  53. }
  54. BUG();
  55. }
  56. static int gpio_read_bit(struct pinmux_data_reg *dr,
  57. unsigned long in_pos)
  58. {
  59. unsigned long pos;
  60. pos = dr->reg_width - (in_pos + 1);
  61. debug("read_bit: addr = %lx, pos = %ld, "
  62. "r_width = %ld\n", dr->reg, pos, dr->reg_width);
  63. return
  64. (gpio_read_raw_reg(dr->mapped_reg + 0x4, dr->reg_width) >> pos) & 1;
  65. }
  66. static void gpio_write_bit(struct pinmux_data_reg *dr,
  67. unsigned long in_pos, unsigned long value)
  68. {
  69. unsigned long pos;
  70. pos = dr->reg_width - (in_pos + 1);
  71. debug("write_bit addr = %lx, value = %d, pos = %ld, "
  72. "r_width = %ld\n",
  73. dr->reg, !!value, pos, dr->reg_width);
  74. if (value)
  75. __set_bit(pos, &dr->reg_shadow);
  76. else
  77. __clear_bit(pos, &dr->reg_shadow);
  78. gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
  79. }
  80. static void config_reg_helper(struct pinmux_info *gpioc,
  81. struct pinmux_cfg_reg *crp,
  82. unsigned long in_pos,
  83. #if 0
  84. void __iomem **mapped_regp,
  85. #else
  86. void **mapped_regp,
  87. #endif
  88. unsigned long *maskp,
  89. unsigned long *posp)
  90. {
  91. int k;
  92. *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
  93. if (crp->field_width) {
  94. *maskp = (1 << crp->field_width) - 1;
  95. *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
  96. } else {
  97. *maskp = (1 << crp->var_field_width[in_pos]) - 1;
  98. *posp = crp->reg_width;
  99. for (k = 0; k <= in_pos; k++)
  100. *posp -= crp->var_field_width[k];
  101. }
  102. }
  103. static int read_config_reg(struct pinmux_info *gpioc,
  104. struct pinmux_cfg_reg *crp,
  105. unsigned long field)
  106. {
  107. void *mapped_reg;
  108. unsigned long mask, pos;
  109. config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
  110. debug("read_reg: addr = %lx, field = %ld, "
  111. "r_width = %ld, f_width = %ld\n",
  112. crp->reg, field, crp->reg_width, crp->field_width);
  113. return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
  114. }
  115. static void write_config_reg(struct pinmux_info *gpioc,
  116. struct pinmux_cfg_reg *crp,
  117. unsigned long field, unsigned long value)
  118. {
  119. void *mapped_reg;
  120. unsigned long mask, pos, data;
  121. config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
  122. debug("write_reg addr = %lx, value = %ld, field = %ld, "
  123. "r_width = %ld, f_width = %ld\n",
  124. crp->reg, value, field, crp->reg_width, crp->field_width);
  125. mask = ~(mask << pos);
  126. value = value << pos;
  127. data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
  128. data &= mask;
  129. data |= value;
  130. if (gpioc->unlock_reg)
  131. gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
  132. 32, ~data);
  133. gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
  134. }
  135. static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
  136. {
  137. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  138. struct pinmux_data_reg *data_reg;
  139. int k, n;
  140. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  141. return -1;
  142. k = 0;
  143. while (1) {
  144. data_reg = gpioc->data_regs + k;
  145. if (!data_reg->reg_width)
  146. break;
  147. data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
  148. for (n = 0; n < data_reg->reg_width; n++) {
  149. if (data_reg->enum_ids[n] == gpiop->enum_id) {
  150. gpiop->flags &= ~PINMUX_FLAG_DREG;
  151. gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
  152. gpiop->flags &= ~PINMUX_FLAG_DBIT;
  153. gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
  154. return 0;
  155. }
  156. }
  157. k++;
  158. }
  159. BUG();
  160. return -1;
  161. }
  162. static void setup_data_regs(struct pinmux_info *gpioc)
  163. {
  164. struct pinmux_data_reg *drp;
  165. int k;
  166. for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
  167. setup_data_reg(gpioc, k);
  168. k = 0;
  169. while (1) {
  170. drp = gpioc->data_regs + k;
  171. if (!drp->reg_width)
  172. break;
  173. drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
  174. drp->reg_width);
  175. k++;
  176. }
  177. }
  178. static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
  179. struct pinmux_data_reg **drp, int *bitp)
  180. {
  181. struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
  182. int k, n;
  183. if (!enum_in_range(gpiop->enum_id, &gpioc->data))
  184. return -1;
  185. k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
  186. n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
  187. *drp = gpioc->data_regs + k;
  188. *bitp = n;
  189. return 0;
  190. }
  191. static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
  192. struct pinmux_cfg_reg **crp,
  193. int *fieldp, int *valuep,
  194. unsigned long **cntp)
  195. {
  196. struct pinmux_cfg_reg *config_reg;
  197. unsigned long r_width, f_width, curr_width, ncomb;
  198. int k, m, n, pos, bit_pos;
  199. k = 0;
  200. while (1) {
  201. config_reg = gpioc->cfg_regs + k;
  202. r_width = config_reg->reg_width;
  203. f_width = config_reg->field_width;
  204. if (!r_width)
  205. break;
  206. pos = 0;
  207. m = 0;
  208. for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
  209. if (f_width)
  210. curr_width = f_width;
  211. else
  212. curr_width = config_reg->var_field_width[m];
  213. ncomb = 1 << curr_width;
  214. for (n = 0; n < ncomb; n++) {
  215. if (config_reg->enum_ids[pos + n] == enum_id) {
  216. *crp = config_reg;
  217. *fieldp = m;
  218. *valuep = n;
  219. *cntp = &config_reg->cnt[m];
  220. return 0;
  221. }
  222. }
  223. pos += ncomb;
  224. m++;
  225. }
  226. k++;
  227. }
  228. return -1;
  229. }
  230. static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
  231. int pos, pinmux_enum_t *enum_idp)
  232. {
  233. pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
  234. pinmux_enum_t *data = gpioc->gpio_data;
  235. int k;
  236. if (!enum_in_range(enum_id, &gpioc->data)) {
  237. if (!enum_in_range(enum_id, &gpioc->mark)) {
  238. debug("non data/mark enum_id for gpio %d\n", gpio);
  239. return -1;
  240. }
  241. }
  242. if (pos) {
  243. *enum_idp = data[pos + 1];
  244. return pos + 1;
  245. }
  246. for (k = 0; k < gpioc->gpio_data_size; k++) {
  247. if (data[k] == enum_id) {
  248. *enum_idp = data[k + 1];
  249. return k + 1;
  250. }
  251. }
  252. debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
  253. return -1;
  254. }
  255. enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
  256. static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
  257. int pinmux_type, int cfg_mode)
  258. {
  259. struct pinmux_cfg_reg *cr = NULL;
  260. pinmux_enum_t enum_id;
  261. struct pinmux_range *range;
  262. int in_range, pos, field, value;
  263. unsigned long *cntp;
  264. switch (pinmux_type) {
  265. case PINMUX_TYPE_FUNCTION:
  266. range = NULL;
  267. break;
  268. case PINMUX_TYPE_OUTPUT:
  269. range = &gpioc->output;
  270. break;
  271. case PINMUX_TYPE_INPUT:
  272. range = &gpioc->input;
  273. break;
  274. case PINMUX_TYPE_INPUT_PULLUP:
  275. range = &gpioc->input_pu;
  276. break;
  277. case PINMUX_TYPE_INPUT_PULLDOWN:
  278. range = &gpioc->input_pd;
  279. break;
  280. default:
  281. goto out_err;
  282. }
  283. pos = 0;
  284. enum_id = 0;
  285. field = 0;
  286. value = 0;
  287. while (1) {
  288. pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
  289. if (pos <= 0)
  290. goto out_err;
  291. if (!enum_id)
  292. break;
  293. /* first check if this is a function enum */
  294. in_range = enum_in_range(enum_id, &gpioc->function);
  295. if (!in_range) {
  296. /* not a function enum */
  297. if (range) {
  298. /*
  299. * other range exists, so this pin is
  300. * a regular GPIO pin that now is being
  301. * bound to a specific direction.
  302. *
  303. * for this case we only allow function enums
  304. * and the enums that match the other range.
  305. */
  306. in_range = enum_in_range(enum_id, range);
  307. /*
  308. * special case pass through for fixed
  309. * input-only or output-only pins without
  310. * function enum register association.
  311. */
  312. if (in_range && enum_id == range->force)
  313. continue;
  314. } else {
  315. /*
  316. * no other range exists, so this pin
  317. * must then be of the function type.
  318. *
  319. * allow function type pins to select
  320. * any combination of function/in/out
  321. * in their MARK lists.
  322. */
  323. in_range = 1;
  324. }
  325. }
  326. if (!in_range)
  327. continue;
  328. if (get_config_reg(gpioc, enum_id, &cr,
  329. &field, &value, &cntp) != 0)
  330. goto out_err;
  331. switch (cfg_mode) {
  332. case GPIO_CFG_DRYRUN:
  333. if (!*cntp ||
  334. (read_config_reg(gpioc, cr, field) != value))
  335. continue;
  336. break;
  337. case GPIO_CFG_REQ:
  338. write_config_reg(gpioc, cr, field, value);
  339. *cntp = *cntp + 1;
  340. break;
  341. case GPIO_CFG_FREE:
  342. *cntp = *cntp - 1;
  343. break;
  344. }
  345. }
  346. return 0;
  347. out_err:
  348. return -1;
  349. }
  350. #if 0
  351. static DEFINE_SPINLOCK(gpio_lock);
  352. static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
  353. {
  354. return container_of(chip, struct pinmux_info, chip);
  355. }
  356. #endif
  357. static int sh_gpio_request(unsigned offset)
  358. {
  359. struct pinmux_data_reg *dummy;
  360. int i, ret, pinmux_type;
  361. ret = -1;
  362. if (!gpioc)
  363. goto err_out;
  364. if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
  365. goto err_out;
  366. /* setup pin function here if no data is associated with pin */
  367. if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
  368. pinmux_type = PINMUX_TYPE_FUNCTION;
  369. else
  370. pinmux_type = PINMUX_TYPE_GPIO;
  371. if (pinmux_type == PINMUX_TYPE_FUNCTION) {
  372. if (pinmux_config_gpio(gpioc, offset,
  373. pinmux_type,
  374. GPIO_CFG_DRYRUN) != 0)
  375. goto err_out;
  376. if (pinmux_config_gpio(gpioc, offset,
  377. pinmux_type,
  378. GPIO_CFG_REQ) != 0)
  379. BUG();
  380. }
  381. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  382. gpioc->gpios[offset].flags |= pinmux_type;
  383. ret = 0;
  384. err_out:
  385. return ret;
  386. }
  387. static void sh_gpio_free(unsigned offset)
  388. {
  389. int pinmux_type;
  390. if (!gpioc)
  391. return;
  392. pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
  393. pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
  394. gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
  395. gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
  396. }
  397. static int pinmux_direction(struct pinmux_info *gpioc,
  398. unsigned gpio, int new_pinmux_type)
  399. {
  400. int pinmux_type;
  401. int ret = -1;
  402. if (!gpioc)
  403. goto err_out;
  404. pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
  405. switch (pinmux_type) {
  406. case PINMUX_TYPE_GPIO:
  407. break;
  408. case PINMUX_TYPE_OUTPUT:
  409. case PINMUX_TYPE_INPUT:
  410. case PINMUX_TYPE_INPUT_PULLUP:
  411. case PINMUX_TYPE_INPUT_PULLDOWN:
  412. pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
  413. break;
  414. default:
  415. goto err_out;
  416. }
  417. if (pinmux_config_gpio(gpioc, gpio,
  418. new_pinmux_type,
  419. GPIO_CFG_DRYRUN) != 0)
  420. goto err_out;
  421. if (pinmux_config_gpio(gpioc, gpio,
  422. new_pinmux_type,
  423. GPIO_CFG_REQ) != 0)
  424. BUG();
  425. gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
  426. gpioc->gpios[gpio].flags |= new_pinmux_type;
  427. ret = 0;
  428. err_out:
  429. return ret;
  430. }
  431. static int sh_gpio_direction_input(unsigned offset)
  432. {
  433. return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
  434. }
  435. static void sh_gpio_set_value(struct pinmux_info *gpioc,
  436. unsigned gpio, int value)
  437. {
  438. struct pinmux_data_reg *dr = NULL;
  439. int bit = 0;
  440. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  441. BUG();
  442. else
  443. gpio_write_bit(dr, bit, value);
  444. }
  445. static int sh_gpio_direction_output(unsigned offset, int value)
  446. {
  447. sh_gpio_set_value(gpioc, offset, value);
  448. return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
  449. }
  450. static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
  451. {
  452. struct pinmux_data_reg *dr = NULL;
  453. int bit = 0;
  454. if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
  455. return -1;
  456. return gpio_read_bit(dr, bit);
  457. }
  458. static int sh_gpio_get(unsigned offset)
  459. {
  460. return sh_gpio_get_value(gpioc, offset);
  461. }
  462. static void sh_gpio_set(unsigned offset, int value)
  463. {
  464. sh_gpio_set_value(gpioc, offset, value);
  465. }
  466. int register_pinmux(struct pinmux_info *pip)
  467. {
  468. if (pip != NULL) {
  469. gpioc = pip;
  470. debug("%s deregistering\n", pip->name);
  471. setup_data_regs(gpioc);
  472. }
  473. return 0;
  474. }
  475. int unregister_pinmux(struct pinmux_info *pip)
  476. {
  477. debug("%s deregistering\n", pip->name);
  478. if (gpioc != pip)
  479. return -1;
  480. gpioc = NULL;
  481. return 0;
  482. }
  483. int gpio_request(unsigned gpio, const char *label)
  484. {
  485. sh_gpio_request(gpio);
  486. return 0;
  487. }
  488. int gpio_free(unsigned gpio)
  489. {
  490. sh_gpio_free(gpio);
  491. return 0;
  492. }
  493. int gpio_direction_input(unsigned gpio)
  494. {
  495. return sh_gpio_direction_input(gpio);
  496. }
  497. int gpio_direction_output(unsigned gpio, int value)
  498. {
  499. return sh_gpio_direction_output(gpio, value);
  500. }
  501. void gpio_set_value(unsigned gpio, int value)
  502. {
  503. sh_gpio_set(gpio, value);
  504. }
  505. int gpio_get_value(unsigned gpio)
  506. {
  507. return sh_gpio_get(gpio);
  508. }