io_interface_mux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /* IO interface mux allocator for ETRAX100LX.
  2. * Copyright 2004, Axis Communications AB
  3. * $Id: io_interface_mux.c,v 1.2 2004/12/21 12:08:38 starvik Exp $
  4. */
  5. /* C.f. ETRAX100LX Designer's Reference 20.9 */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <asm/arch/svinto.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/io_interface_mux.h>
  14. #define DBG(s)
  15. /* Macro to access ETRAX 100 registers */
  16. #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  17. IO_STATE_(reg##_, field##_, _##val)
  18. enum io_if_group {
  19. group_a = (1<<0),
  20. group_b = (1<<1),
  21. group_c = (1<<2),
  22. group_d = (1<<3),
  23. group_e = (1<<4),
  24. group_f = (1<<5)
  25. };
  26. struct watcher
  27. {
  28. void (*notify)(const unsigned int gpio_in_available,
  29. const unsigned int gpio_out_available,
  30. const unsigned char pa_available,
  31. const unsigned char pb_available);
  32. struct watcher *next;
  33. };
  34. struct if_group
  35. {
  36. enum io_if_group group;
  37. unsigned char used;
  38. enum cris_io_interface owner;
  39. };
  40. struct interface
  41. {
  42. enum cris_io_interface ioif;
  43. unsigned char groups;
  44. unsigned char used;
  45. char *owner;
  46. unsigned int gpio_g_in;
  47. unsigned int gpio_g_out;
  48. unsigned char gpio_b;
  49. };
  50. static struct if_group if_groups[6] = {
  51. {
  52. .group = group_a,
  53. .used = 0,
  54. },
  55. {
  56. .group = group_b,
  57. .used = 0,
  58. },
  59. {
  60. .group = group_c,
  61. .used = 0,
  62. },
  63. {
  64. .group = group_d,
  65. .used = 0,
  66. },
  67. {
  68. .group = group_e,
  69. .used = 0,
  70. },
  71. {
  72. .group = group_f,
  73. .used = 0,
  74. }
  75. };
  76. /* The order in the array must match the order of enum
  77. * cris_io_interface in io_interface_mux.h */
  78. static struct interface interfaces[] = {
  79. /* Begin Non-multiplexed interfaces */
  80. {
  81. .ioif = if_eth,
  82. .groups = 0,
  83. .gpio_g_in = 0,
  84. .gpio_g_out = 0,
  85. .gpio_b = 0
  86. },
  87. {
  88. .ioif = if_serial_0,
  89. .groups = 0,
  90. .gpio_g_in = 0,
  91. .gpio_g_out = 0,
  92. .gpio_b = 0
  93. },
  94. /* End Non-multiplexed interfaces */
  95. {
  96. .ioif = if_serial_1,
  97. .groups = group_e,
  98. .gpio_g_in = 0x00000000,
  99. .gpio_g_out = 0x00000000,
  100. .gpio_b = 0x00
  101. },
  102. {
  103. .ioif = if_serial_2,
  104. .groups = group_b,
  105. .gpio_g_in = 0x000000c0,
  106. .gpio_g_out = 0x000000c0,
  107. .gpio_b = 0x00
  108. },
  109. {
  110. .ioif = if_serial_3,
  111. .groups = group_c,
  112. .gpio_g_in = 0xc0000000,
  113. .gpio_g_out = 0xc0000000,
  114. .gpio_b = 0x00
  115. },
  116. {
  117. .ioif = if_sync_serial_1,
  118. .groups = group_e | group_f, /* if_sync_serial_1 and if_sync_serial_3
  119. can be used simultaneously */
  120. .gpio_g_in = 0x00000000,
  121. .gpio_g_out = 0x00000000,
  122. .gpio_b = 0x10
  123. },
  124. {
  125. .ioif = if_sync_serial_3,
  126. .groups = group_c | group_f,
  127. .gpio_g_in = 0xc0000000,
  128. .gpio_g_out = 0xc0000000,
  129. .gpio_b = 0x80
  130. },
  131. {
  132. .ioif = if_shared_ram,
  133. .groups = group_a,
  134. .gpio_g_in = 0x0000ff3e,
  135. .gpio_g_out = 0x0000ff38,
  136. .gpio_b = 0x00
  137. },
  138. {
  139. .ioif = if_shared_ram_w,
  140. .groups = group_a | group_d,
  141. .gpio_g_in = 0x00ffff3e,
  142. .gpio_g_out = 0x00ffff38,
  143. .gpio_b = 0x00
  144. },
  145. {
  146. .ioif = if_par_0,
  147. .groups = group_a,
  148. .gpio_g_in = 0x0000ff3e,
  149. .gpio_g_out = 0x0000ff3e,
  150. .gpio_b = 0x00
  151. },
  152. {
  153. .ioif = if_par_1,
  154. .groups = group_d,
  155. .gpio_g_in = 0x3eff0000,
  156. .gpio_g_out = 0x3eff0000,
  157. .gpio_b = 0x00
  158. },
  159. {
  160. .ioif = if_par_w,
  161. .groups = group_a | group_d,
  162. .gpio_g_in = 0x00ffff3e,
  163. .gpio_g_out = 0x00ffff3e,
  164. .gpio_b = 0x00
  165. },
  166. {
  167. .ioif = if_scsi8_0,
  168. .groups = group_a | group_b | group_f, /* if_scsi8_0 and if_scsi8_1
  169. can be used simultaneously */
  170. .gpio_g_in = 0x0000ffff,
  171. .gpio_g_out = 0x0000ffff,
  172. .gpio_b = 0x10
  173. },
  174. {
  175. .ioif = if_scsi8_1,
  176. .groups = group_c | group_d | group_f, /* if_scsi8_0 and if_scsi8_1
  177. can be used simultaneously */
  178. .gpio_g_in = 0xffff0000,
  179. .gpio_g_out = 0xffff0000,
  180. .gpio_b = 0x80
  181. },
  182. {
  183. .ioif = if_scsi_w,
  184. .groups = group_a | group_b | group_d | group_f,
  185. .gpio_g_in = 0x01ffffff,
  186. .gpio_g_out = 0x07ffffff,
  187. .gpio_b = 0x80
  188. },
  189. {
  190. .ioif = if_ata,
  191. .groups = group_a | group_b | group_c | group_d,
  192. .gpio_g_in = 0xf9ffffff,
  193. .gpio_g_out = 0xffffffff,
  194. .gpio_b = 0x80
  195. },
  196. {
  197. .ioif = if_csp,
  198. .groups = group_f, /* if_csp and if_i2c can be used simultaneously */
  199. .gpio_g_in = 0x00000000,
  200. .gpio_g_out = 0x00000000,
  201. .gpio_b = 0xfc
  202. },
  203. {
  204. .ioif = if_i2c,
  205. .groups = group_f, /* if_csp and if_i2c can be used simultaneously */
  206. .gpio_g_in = 0x00000000,
  207. .gpio_g_out = 0x00000000,
  208. .gpio_b = 0x03
  209. },
  210. {
  211. .ioif = if_usb_1,
  212. .groups = group_e | group_f,
  213. .gpio_g_in = 0x00000000,
  214. .gpio_g_out = 0x00000000,
  215. .gpio_b = 0x2c
  216. },
  217. {
  218. .ioif = if_usb_2,
  219. .groups = group_d,
  220. .gpio_g_in = 0x0e000000,
  221. .gpio_g_out = 0x3c000000,
  222. .gpio_b = 0x00
  223. },
  224. /* GPIO pins */
  225. {
  226. .ioif = if_gpio_grp_a,
  227. .groups = group_a,
  228. .gpio_g_in = 0x0000ff3f,
  229. .gpio_g_out = 0x0000ff3f,
  230. .gpio_b = 0x00
  231. },
  232. {
  233. .ioif = if_gpio_grp_b,
  234. .groups = group_b,
  235. .gpio_g_in = 0x000000c0,
  236. .gpio_g_out = 0x000000c0,
  237. .gpio_b = 0x00
  238. },
  239. {
  240. .ioif = if_gpio_grp_c,
  241. .groups = group_c,
  242. .gpio_g_in = 0xc0000000,
  243. .gpio_g_out = 0xc0000000,
  244. .gpio_b = 0x00
  245. },
  246. {
  247. .ioif = if_gpio_grp_d,
  248. .groups = group_d,
  249. .gpio_g_in = 0x3fff0000,
  250. .gpio_g_out = 0x3fff0000,
  251. .gpio_b = 0x00
  252. },
  253. {
  254. .ioif = if_gpio_grp_e,
  255. .groups = group_e,
  256. .gpio_g_in = 0x00000000,
  257. .gpio_g_out = 0x00000000,
  258. .gpio_b = 0x00
  259. },
  260. {
  261. .ioif = if_gpio_grp_f,
  262. .groups = group_f,
  263. .gpio_g_in = 0x00000000,
  264. .gpio_g_out = 0x00000000,
  265. .gpio_b = 0xff
  266. }
  267. /* Array end */
  268. };
  269. static struct watcher *watchers = NULL;
  270. static unsigned int gpio_in_pins = 0xffffffff;
  271. static unsigned int gpio_out_pins = 0xffffffff;
  272. static unsigned char gpio_pb_pins = 0xff;
  273. static unsigned char gpio_pa_pins = 0xff;
  274. static enum cris_io_interface gpio_pa_owners[8];
  275. static enum cris_io_interface gpio_pb_owners[8];
  276. static enum cris_io_interface gpio_pg_owners[32];
  277. static int cris_io_interface_init(void);
  278. static unsigned char clear_group_from_set(const unsigned char groups, struct if_group *group)
  279. {
  280. return (groups & ~group->group);
  281. }
  282. static struct if_group *get_group(const unsigned char groups)
  283. {
  284. int i;
  285. for (i = 0; i < ARRAY_SIZE(if_groups); i++) {
  286. if (groups & if_groups[i].group) {
  287. return &if_groups[i];
  288. }
  289. }
  290. return NULL;
  291. }
  292. static void notify_watchers(void)
  293. {
  294. struct watcher *w = watchers;
  295. DBG(printk("io_interface_mux: notifying watchers\n"));
  296. while (NULL != w) {
  297. w->notify((const unsigned int)gpio_in_pins,
  298. (const unsigned int)gpio_out_pins,
  299. (const unsigned char)gpio_pa_pins,
  300. (const unsigned char)gpio_pb_pins);
  301. w = w->next;
  302. }
  303. }
  304. int cris_request_io_interface(enum cris_io_interface ioif, const char *device_id)
  305. {
  306. int set_gen_config = 0;
  307. int set_gen_config_ii = 0;
  308. unsigned long int gens;
  309. unsigned long int gens_ii;
  310. struct if_group *grp;
  311. unsigned char group_set;
  312. unsigned long flags;
  313. (void)cris_io_interface_init();
  314. DBG(printk("cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
  315. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  316. printk(KERN_CRIT "cris_request_io_interface: Bad interface %u submitted for %s\n",
  317. ioif,
  318. device_id);
  319. return -EINVAL;
  320. }
  321. local_irq_save(flags);
  322. if (interfaces[ioif].used) {
  323. local_irq_restore(flags);
  324. printk(KERN_CRIT "cris_io_interface: Cannot allocate interface for %s, in use by %s\n",
  325. device_id,
  326. interfaces[ioif].owner);
  327. return -EBUSY;
  328. }
  329. /* Check that all required groups are free before allocating, */
  330. group_set = interfaces[ioif].groups;
  331. while (NULL != (grp = get_group(group_set))) {
  332. if (grp->used) {
  333. if (grp->group == group_f) {
  334. if ((if_sync_serial_1 == ioif) ||
  335. (if_sync_serial_3 == ioif)) {
  336. if ((grp->owner != if_sync_serial_1) &&
  337. (grp->owner != if_sync_serial_3)) {
  338. local_irq_restore(flags);
  339. return -EBUSY;
  340. }
  341. } else if ((if_scsi8_0 == ioif) ||
  342. (if_scsi8_1 == ioif)) {
  343. if ((grp->owner != if_scsi8_0) &&
  344. (grp->owner != if_scsi8_1)) {
  345. local_irq_restore(flags);
  346. return -EBUSY;
  347. }
  348. }
  349. } else {
  350. local_irq_restore(flags);
  351. return -EBUSY;
  352. }
  353. }
  354. group_set = clear_group_from_set(group_set, grp);
  355. }
  356. /* Are the required GPIO pins available too? */
  357. if (((interfaces[ioif].gpio_g_in & gpio_in_pins) != interfaces[ioif].gpio_g_in) ||
  358. ((interfaces[ioif].gpio_g_out & gpio_out_pins) != interfaces[ioif].gpio_g_out) ||
  359. ((interfaces[ioif].gpio_b & gpio_pb_pins) != interfaces[ioif].gpio_b)) {
  360. local_irq_restore(flags);
  361. printk(KERN_CRIT "cris_request_io_interface: Could not get required pins for interface %u\n",
  362. ioif);
  363. return -EBUSY;
  364. }
  365. /* All needed I/O pins and pin groups are free, allocate. */
  366. group_set = interfaces[ioif].groups;
  367. while (NULL != (grp = get_group(group_set))) {
  368. grp->used = 1;
  369. grp->owner = ioif;
  370. group_set = clear_group_from_set(group_set, grp);
  371. }
  372. gens = genconfig_shadow;
  373. gens_ii = gen_config_ii_shadow;
  374. set_gen_config = 1;
  375. switch (ioif)
  376. {
  377. /* Begin Non-multiplexed interfaces */
  378. case if_eth:
  379. /* fall through */
  380. case if_serial_0:
  381. set_gen_config = 0;
  382. break;
  383. /* End Non-multiplexed interfaces */
  384. case if_serial_1:
  385. set_gen_config_ii = 1;
  386. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, async);
  387. break;
  388. case if_serial_2:
  389. SETS(gens, R_GEN_CONFIG, ser2, select);
  390. break;
  391. case if_serial_3:
  392. SETS(gens, R_GEN_CONFIG, ser3, select);
  393. set_gen_config_ii = 1;
  394. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, async);
  395. break;
  396. case if_sync_serial_1:
  397. set_gen_config_ii = 1;
  398. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, sync);
  399. break;
  400. case if_sync_serial_3:
  401. SETS(gens, R_GEN_CONFIG, ser3, select);
  402. set_gen_config_ii = 1;
  403. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, sync);
  404. break;
  405. case if_shared_ram:
  406. SETS(gens, R_GEN_CONFIG, mio, select);
  407. break;
  408. case if_shared_ram_w:
  409. SETS(gens, R_GEN_CONFIG, mio_w, select);
  410. break;
  411. case if_par_0:
  412. SETS(gens, R_GEN_CONFIG, par0, select);
  413. break;
  414. case if_par_1:
  415. SETS(gens, R_GEN_CONFIG, par1, select);
  416. break;
  417. case if_par_w:
  418. SETS(gens, R_GEN_CONFIG, par0, select);
  419. SETS(gens, R_GEN_CONFIG, par_w, select);
  420. break;
  421. case if_scsi8_0:
  422. SETS(gens, R_GEN_CONFIG, scsi0, select);
  423. break;
  424. case if_scsi8_1:
  425. SETS(gens, R_GEN_CONFIG, scsi1, select);
  426. break;
  427. case if_scsi_w:
  428. SETS(gens, R_GEN_CONFIG, scsi0, select);
  429. SETS(gens, R_GEN_CONFIG, scsi0w, select);
  430. break;
  431. case if_ata:
  432. SETS(gens, R_GEN_CONFIG, ata, select);
  433. break;
  434. case if_csp:
  435. /* fall through */
  436. case if_i2c:
  437. set_gen_config = 0;
  438. break;
  439. case if_usb_1:
  440. SETS(gens, R_GEN_CONFIG, usb1, select);
  441. break;
  442. case if_usb_2:
  443. SETS(gens, R_GEN_CONFIG, usb2, select);
  444. break;
  445. case if_gpio_grp_a:
  446. /* GPIO groups are only accounted, don't do configuration changes. */
  447. /* fall through */
  448. case if_gpio_grp_b:
  449. /* fall through */
  450. case if_gpio_grp_c:
  451. /* fall through */
  452. case if_gpio_grp_d:
  453. /* fall through */
  454. case if_gpio_grp_e:
  455. /* fall through */
  456. case if_gpio_grp_f:
  457. set_gen_config = 0;
  458. break;
  459. default:
  460. panic("cris_request_io_interface: Bad interface %u submitted for %s\n",
  461. ioif,
  462. device_id);
  463. }
  464. interfaces[ioif].used = 1;
  465. interfaces[ioif].owner = (char*)device_id;
  466. if (set_gen_config) {
  467. volatile int i;
  468. genconfig_shadow = gens;
  469. *R_GEN_CONFIG = genconfig_shadow;
  470. /* Wait 12 cycles before doing any DMA command */
  471. for(i = 6; i > 0; i--)
  472. nop();
  473. }
  474. if (set_gen_config_ii) {
  475. gen_config_ii_shadow = gens_ii;
  476. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  477. }
  478. DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  479. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  480. DBG(printk("grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  481. interfaces[ioif].gpio_g_in,
  482. interfaces[ioif].gpio_g_out,
  483. interfaces[ioif].gpio_b));
  484. gpio_in_pins &= ~interfaces[ioif].gpio_g_in;
  485. gpio_out_pins &= ~interfaces[ioif].gpio_g_out;
  486. gpio_pb_pins &= ~interfaces[ioif].gpio_b;
  487. DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  488. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  489. local_irq_restore(flags);
  490. notify_watchers();
  491. return 0;
  492. }
  493. void cris_free_io_interface(enum cris_io_interface ioif)
  494. {
  495. struct if_group *grp;
  496. unsigned char group_set;
  497. unsigned long flags;
  498. (void)cris_io_interface_init();
  499. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  500. printk(KERN_CRIT "cris_free_io_interface: Bad interface %u\n",
  501. ioif);
  502. return;
  503. }
  504. local_irq_save(flags);
  505. if (!interfaces[ioif].used) {
  506. printk(KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
  507. ioif);
  508. local_irq_restore(flags);
  509. return;
  510. }
  511. group_set = interfaces[ioif].groups;
  512. while (NULL != (grp = get_group(group_set))) {
  513. if (grp->group == group_f) {
  514. switch (ioif)
  515. {
  516. case if_sync_serial_1:
  517. if ((grp->owner == if_sync_serial_1) &&
  518. interfaces[if_sync_serial_3].used) {
  519. grp->owner = if_sync_serial_3;
  520. } else
  521. grp->used = 0;
  522. break;
  523. case if_sync_serial_3:
  524. if ((grp->owner == if_sync_serial_3) &&
  525. interfaces[if_sync_serial_1].used) {
  526. grp->owner = if_sync_serial_1;
  527. } else
  528. grp->used = 0;
  529. break;
  530. case if_scsi8_0:
  531. if ((grp->owner == if_scsi8_0) &&
  532. interfaces[if_scsi8_1].used) {
  533. grp->owner = if_scsi8_1;
  534. } else
  535. grp->used = 0;
  536. break;
  537. case if_scsi8_1:
  538. if ((grp->owner == if_scsi8_1) &&
  539. interfaces[if_scsi8_0].used) {
  540. grp->owner = if_scsi8_0;
  541. } else
  542. grp->used = 0;
  543. break;
  544. default:
  545. grp->used = 0;
  546. }
  547. } else {
  548. grp->used = 0;
  549. }
  550. group_set = clear_group_from_set(group_set, grp);
  551. }
  552. interfaces[ioif].used = 0;
  553. interfaces[ioif].owner = NULL;
  554. DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  555. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  556. DBG(printk("freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  557. interfaces[ioif].gpio_g_in,
  558. interfaces[ioif].gpio_g_out,
  559. interfaces[ioif].gpio_b));
  560. gpio_in_pins |= interfaces[ioif].gpio_g_in;
  561. gpio_out_pins |= interfaces[ioif].gpio_g_out;
  562. gpio_pb_pins |= interfaces[ioif].gpio_b;
  563. DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  564. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  565. local_irq_restore(flags);
  566. notify_watchers();
  567. }
  568. /* Create a bitmask from bit 0 (inclusive) to bit stop_bit
  569. (non-inclusive). stop_bit == 0 returns 0x0 */
  570. static inline unsigned int create_mask(const unsigned stop_bit)
  571. {
  572. /* Avoid overflow */
  573. if (stop_bit >= 32) {
  574. return 0xffffffff;
  575. }
  576. return (1<<stop_bit)-1;
  577. }
  578. /* port can be 'a', 'b' or 'g' */
  579. int cris_io_interface_allocate_pins(const enum cris_io_interface ioif,
  580. const char port,
  581. const unsigned start_bit,
  582. const unsigned stop_bit)
  583. {
  584. unsigned int i;
  585. unsigned int mask = 0;
  586. unsigned int tmp_mask;
  587. unsigned long int flags;
  588. enum cris_io_interface *owners;
  589. (void)cris_io_interface_init();
  590. DBG(printk("cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
  591. ioif, port, start_bit, stop_bit));
  592. if (!((start_bit <= stop_bit) &&
  593. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  594. ((port == 'g') && (stop_bit < 32))))) {
  595. return -EINVAL;
  596. }
  597. mask = create_mask(stop_bit + 1);
  598. tmp_mask = create_mask(start_bit);
  599. mask &= ~tmp_mask;
  600. DBG(printk("cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  601. port, start_bit, stop_bit, mask));
  602. local_irq_save(flags);
  603. switch (port) {
  604. case 'a':
  605. if ((gpio_pa_pins & mask) != mask) {
  606. local_irq_restore(flags);
  607. return -EBUSY;
  608. }
  609. owners = gpio_pa_owners;
  610. gpio_pa_pins &= ~mask;
  611. break;
  612. case 'b':
  613. if ((gpio_pb_pins & mask) != mask) {
  614. local_irq_restore(flags);
  615. return -EBUSY;
  616. }
  617. owners = gpio_pb_owners;
  618. gpio_pb_pins &= ~mask;
  619. break;
  620. case 'g':
  621. if (((gpio_in_pins & mask) != mask) ||
  622. ((gpio_out_pins & mask) != mask)) {
  623. local_irq_restore(flags);
  624. return -EBUSY;
  625. }
  626. owners = gpio_pg_owners;
  627. gpio_in_pins &= ~mask;
  628. gpio_out_pins &= ~mask;
  629. break;
  630. default:
  631. local_irq_restore(flags);
  632. return -EINVAL;
  633. }
  634. for (i = start_bit; i <= stop_bit; i++) {
  635. owners[i] = ioif;
  636. }
  637. local_irq_restore(flags);
  638. notify_watchers();
  639. return 0;
  640. }
  641. /* port can be 'a', 'b' or 'g' */
  642. int cris_io_interface_free_pins(const enum cris_io_interface ioif,
  643. const char port,
  644. const unsigned start_bit,
  645. const unsigned stop_bit)
  646. {
  647. unsigned int i;
  648. unsigned int mask = 0;
  649. unsigned int tmp_mask;
  650. unsigned long int flags;
  651. enum cris_io_interface *owners;
  652. (void)cris_io_interface_init();
  653. if (!((start_bit <= stop_bit) &&
  654. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  655. ((port == 'g') && (stop_bit < 32))))) {
  656. return -EINVAL;
  657. }
  658. mask = create_mask(stop_bit + 1);
  659. tmp_mask = create_mask(start_bit);
  660. mask &= ~tmp_mask;
  661. DBG(printk("cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  662. port, start_bit, stop_bit, mask));
  663. local_irq_save(flags);
  664. switch (port) {
  665. case 'a':
  666. if ((~gpio_pa_pins & mask) != mask) {
  667. local_irq_restore(flags);
  668. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  669. }
  670. owners = gpio_pa_owners;
  671. break;
  672. case 'b':
  673. if ((~gpio_pb_pins & mask) != mask) {
  674. local_irq_restore(flags);
  675. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  676. }
  677. owners = gpio_pb_owners;
  678. break;
  679. case 'g':
  680. if (((~gpio_in_pins & mask) != mask) ||
  681. ((~gpio_out_pins & mask) != mask)) {
  682. local_irq_restore(flags);
  683. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  684. }
  685. owners = gpio_pg_owners;
  686. break;
  687. default:
  688. owners = NULL; /* Cannot happen. Shut up, gcc! */
  689. }
  690. for (i = start_bit; i <= stop_bit; i++) {
  691. if (owners[i] != ioif) {
  692. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
  693. }
  694. }
  695. /* All was ok, change data. */
  696. switch (port) {
  697. case 'a':
  698. gpio_pa_pins |= mask;
  699. break;
  700. case 'b':
  701. gpio_pb_pins |= mask;
  702. break;
  703. case 'g':
  704. gpio_in_pins |= mask;
  705. gpio_out_pins |= mask;
  706. break;
  707. }
  708. for (i = start_bit; i <= stop_bit; i++) {
  709. owners[i] = if_unclaimed;
  710. }
  711. local_irq_restore(flags);
  712. notify_watchers();
  713. return 0;
  714. }
  715. int cris_io_interface_register_watcher(void (*notify)(const unsigned int gpio_in_available,
  716. const unsigned int gpio_out_available,
  717. const unsigned char pa_available,
  718. const unsigned char pb_available))
  719. {
  720. struct watcher *w;
  721. (void)cris_io_interface_init();
  722. if (NULL == notify) {
  723. return -EINVAL;
  724. }
  725. w = kmalloc(sizeof(*w), GFP_KERNEL);
  726. if (!w) {
  727. return -ENOMEM;
  728. }
  729. w->notify = notify;
  730. w->next = watchers;
  731. watchers = w;
  732. w->notify((const unsigned int)gpio_in_pins,
  733. (const unsigned int)gpio_out_pins,
  734. (const unsigned char)gpio_pa_pins,
  735. (const unsigned char)gpio_pb_pins);
  736. return 0;
  737. }
  738. void cris_io_interface_delete_watcher(void (*notify)(const unsigned int gpio_in_available,
  739. const unsigned int gpio_out_available,
  740. const unsigned char pa_available,
  741. const unsigned char pb_available))
  742. {
  743. struct watcher *w = watchers, *prev = NULL;
  744. (void)cris_io_interface_init();
  745. while ((NULL != w) && (w->notify != notify)){
  746. prev = w;
  747. w = w->next;
  748. }
  749. if (NULL != w) {
  750. if (NULL != prev) {
  751. prev->next = w->next;
  752. } else {
  753. watchers = w->next;
  754. }
  755. kfree(w);
  756. return;
  757. }
  758. printk(KERN_WARNING "cris_io_interface_delete_watcher: Deleting unknown watcher 0x%p\n", notify);
  759. }
  760. static int cris_io_interface_init(void)
  761. {
  762. static int first = 1;
  763. int i;
  764. if (!first) {
  765. return 0;
  766. }
  767. first = 0;
  768. for (i = 0; i<8; i++) {
  769. gpio_pa_owners[i] = if_unclaimed;
  770. gpio_pb_owners[i] = if_unclaimed;
  771. gpio_pg_owners[i] = if_unclaimed;
  772. }
  773. for (; i<32; i++) {
  774. gpio_pg_owners[i] = if_unclaimed;
  775. }
  776. return 0;
  777. }
  778. module_init(cris_io_interface_init);
  779. EXPORT_SYMBOL(cris_request_io_interface);
  780. EXPORT_SYMBOL(cris_free_io_interface);
  781. EXPORT_SYMBOL(cris_io_interface_allocate_pins);
  782. EXPORT_SYMBOL(cris_io_interface_free_pins);
  783. EXPORT_SYMBOL(cris_io_interface_register_watcher);
  784. EXPORT_SYMBOL(cris_io_interface_delete_watcher);